1、構建項目結構如下 2、構建項目中的pom.xml文件中的依賴資源,裡面包含ajax和分頁插件的依賴哦 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" x ...
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 = "/hello",method = {RequestMethod.POST})
public String index2(Model model){
model.addAttribute("msg", "hello!");
return "test";
}
但瀏覽器地址欄進行訪問是通過GET方式進行的 ,我們定義的是POST方法-所以報錯不匹配
改為GET
@RequestMapping(value = "/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;
}