Restful
1.REST架構的主要原則
1.1 對網路上所有的資源都有一個資源標志符
1.2 對資源的操作不會改變識別符號
1.3 同一資源有多種表現形式(xml、json)、
1.4 所有操作都是無狀態的(Stateless)
符合上述REST原則的架構方式稱為Restful
2.URI和URL區別
URI:http://example.com/users/
URL:http://example.com/users/{user} (one for each user)
2.1.什么是無狀態性
? 使得客戶端和服務器端不必保存對方的詳細資訊,服務器只需要處理當前的請求,不需了解請求的歷史,可以更容易的釋放資源,讓服務器利用Pool(連接池)技術來提高穩定性和性能,
3.Restful操作
RESTful是一種常見的REST應用,是遵循REST風格的web服務,REST式的web服務是一種ROA(面向資源的架構),更加安全!!
| http方法 | 資源操作 | 冪等 | 安全 |
|---|---|---|---|
| GET | SELECT | 是 | 是 |
| POST | INSERT | 否 | 否 |
| PUT | UPDATE | 是 | 否 |
| DELETE | DELETE | 是 | 否 |
注:冪等性:對同一REST介面的多次訪問,得到的資源狀態是相同的,
? 安全性:對該REST介面訪問,不會使服務器端資源的狀態發生改變,
3.1介面示例
傳統URL請求格式:
http:/127.0.0.1/test/query/2 http:/127.0.0.1/test/query?id=1 GET 根據用戶id查詢用戶資料
http:/127.0.0.1/test/save POST 新增用戶
http:/127.0.0.1/test/update POST 修改用戶資訊
http:/127.0.0.1/test/delete GET/POST 洗掉用戶資訊
RESTful請求格式:
http:/127.0.0.1/test/1 GET 根據用戶id查詢用戶資料
http:/127.0.0.1/test POST 新增用戶
http:/127.0.0.1/test PUT 修改用戶資訊
http:/127.0.0.1/test/1 DELETE 洗掉用戶資訊
4.Http狀態碼
4.1一般情況:
200:請求回應成功 200
3xx:請求重定向 重定向:你重新到我給你新位置去;
4xx:找不到資源 404 資源不存在;
5xx:服務器代碼錯誤 500 502:網關錯誤
4.2具體情況:
HttpStatus = {
//Informational 1xx 資訊
'100' : 'Continue', //繼續
'101' : 'Switching Protocols', //交換協議
//Successful 2xx 成功
'200' : 'OK', //OK
'201' : 'Created', //創建
'202' : 'Accepted', //已接受
'203' : 'Non-Authoritative Information', //非權威資訊
'204' : 'No Content', //成功,但沒有內容
'205' : 'Reset Content', //重置內容
'206' : 'Partial Content', //部分內容
//Redirection 3xx 重定向
'300' : 'Multiple Choices', //多種選擇
'301' : 'Moved Permanently', //永久移動
'302' : 'Found', //找到
'303' : 'See Other', //參見其他
'304' : 'Not Modified', //未修改
'305' : 'Use Proxy', //使用代理
'306' : 'Unused', //未使用
'307' : 'Temporary Redirect', //暫時重定向
//Client Error 4xx 客戶端錯誤
'400' : 'Bad Request', //錯誤的請求
'401' : 'Unauthorized', //未經授權
'402' : 'Payment Required', //付費請求
'403' : 'Forbidden', //禁止
'404' : 'Not Found', //沒有找到
'405' : 'Method Not Allowed', //方法不允許
'406' : 'Not Acceptable', //不可接受
'407' : 'Proxy Authentication Required', //需要代理身份驗證
'408' : 'Request Timeout', //請求超時
'409' : 'Conflict', //指令沖突
'410' : 'Gone', //檔案永久地離開了指定的位置
'411' : 'Length Required', //需要Content-Length頭請求
'412' : 'Precondition Failed', //前提條件失敗
'413' : 'Request Entity Too Large', //請求物體太大
'414' : 'Request-URI Too Long', //請求URI太長
'415' : 'Unsupported Media Type', //不支持的媒體型別
'416' : 'Requested Range Not Satisfiable', //請求的范圍不可滿足
'417' : 'Expectation Failed', //期望失敗
//Server Error 5xx 服務器錯誤
'500' : 'Internal Server Error', //內部服務器錯誤
'501' : 'Not Implemented', //未實作
'502' : 'Bad Gateway', //錯誤的網關
'503' : 'Service Unavailable', //服務不可用
'504' : 'Gateway Timeout', //網關超時
'505' : 'HTTP Version Not Supported' //HTTP版本不支持
};
5.測驗
@Controller
public class RestFulController {
//映射訪問路徑
@RequestMapping("/commit/{p1}/{p2}")
//在SpringMVC中可以使用 @PathVariable,讓方法引數的值對應系結到一個URI變數上
public ModelAndView index(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
int result = p1 + p2;
//實體化一個ModelAndView物件用于向視圖中傳值
mv.addObject("msg","結果:" + result);
//回傳視圖
mv.setViewName("test");
return mv;
}
}

也可以使用method屬性指定型別
@RequestMapping(value = "https://www.cnblogs.com/hello",method = {RequestMethod.POST})
public String index2(Model model){
model.addAttribute("msg", "hello!");
return "test";
}

但瀏覽器地址欄進行訪問是通過GET方式進行的 ,我們定義的是POST方法-所以報錯不匹配
改為GET
@RequestMapping(value = "https://www.cnblogs.com/hello",method = {RequestMethod.GET})
//我們一般采用這種方式:@GetMapping("/hello")
public String index2(Model model){
model.addAttribute("msg", "hello!");
return "test";
}

我們可以看到Method 方式太長,不方便,我們可以用延伸的方法
@GetMapping:扮演的是@RequestMapping(method =RequestMethod.GET) 的快捷方式,
@PostMapping (method =RequestMethod.GET)
@PutMapping (method =RequestMethod.POST)
@DeleteMapping (method =RequestMethod.DELETE)
@PatchMapping (method =RequestMethod.PATCH)
如:
如果是地址欄輸入(GET)可以呼叫 index方法:
@GetMapping("/commit/{p1}/{p2}")
//在SpringMVC中可以使用 @PathVariable,讓方法引數的值對應系結到一個URI變數上
public ModelAndView index(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
int result = p1 + p2;
//實體化一個ModelAndView物件用于向視圖中傳值
mv.addObject("msg","結果1:" + result);
//回傳視圖
mv.setViewName("test");
return mv;
}
如果前端頁面form表單 method為POST,則呼叫index2方法
@PostMapping("/commit/{p1}/{p2}")
//在SpringMVC中可以使用 @PathVariable,讓方法引數的值對應系結到一個URI變數上
public ModelAndView index2(@PathVariable int p1, @PathVariable int p2, ModelAndView mv){
int result = p1 + p2;
//實體化一個ModelAndView物件用于向視圖中傳值
mv.addObject("msg","結果2:" + result);
//回傳視圖
mv.setViewName("test");
return mv;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502304.html
標籤:其他
上一篇:計算機基礎之編程語言
下一篇:串列型別
