# RestFul 風格 ###### tags: `SpringMVC-基礎` ## 概念 Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。 ## 功能 资源:互联网所有的事物都可以被抽象为资源 资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。 分别对应 添加、 删除、修改、查询。 ### 传统方式操作资源 :通过不同的参数来实现不同的效果!方法单一,post 和 get http://127.0.0.1/item/queryItem.action?id=1 查询,GET http://127.0.0.1/item/saveItem.action 新增,POST http://127.0.0.1/item/updateItem.action 更新,POST http://127.0.0.1/item/deleteItem.action?id=1 删除,GET或POST ### 使用RESTful操作资源 :可以通过不同的【请求方式】来实现不同的效果!如下:请求地址一样,但是功能可以不同! http://127.0.0.1/item/1 查询,GET http://127.0.0.1/item 新增,POST http://127.0.0.1/item 更新,PUT http://127.0.0.1/item/1 删除,DELETE #### 測試:原來的方式 ```java= @Controller public class RestFulController { @RequestMapping("/add") public String test1(int a, int b, Model model){ int res = a + b; model.addAttribute("msg","結果為" + res); return "test"; } } ``` 報出500錯誤 ![](https://i.imgur.com/1yPPSsg.png) 改成在網址欄輸入a與b的參數 成功 ![](https://i.imgur.com/sBvJwt7.png) ### 使用RestFul風格 在SpringMVC中可以使用@PathVariable註解,讓方法參數的值對應綁定到一個URL模板變量上 ```java= @Controller public class RestFulController { //原來的:http://localhost:8080/add?a=1&b=2 //RestFul:http://localhost:8080/add/a/b @RequestMapping("/add/{a}/{b}") public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果為" + res); return "test"; } } ``` 在網址欄不需要使用?跟&拼接參數,只要根據參數順序輸入【/參數a/參數b】即可 ![](https://i.imgur.com/Qs5Cvq0.png) ### 使用method屬性指定請求類型 用於約束請求的類型,可以收窄請求範圍。指定請求謂詞的類型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等 ```java= @Controller public class RestFulController { //原來的:http://localhost:8080/add?a=1&b=2 //RestFul:http://localhost:8080/add/a/b @RequestMapping(value="/add/{a}/{b}",method= RequestMethod.DELETE) public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果為" + res); return "test"; } } ``` 爆出405錯誤,Method Not Allowed(方法不被允許) 因為我們是請求GET方法,但是程式碼卻是寫DELETE方式 ![](https://i.imgur.com/UQ0Qe4A.png) 更改請求方式為GET後,就能成功運行 ```java= @RequestMapping(value="/add/{a}/{b}",method= RequestMethod.GET) public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果為" + res); return "test"; } } ``` ![](https://i.imgur.com/ehLfTue.png) ### 簡易的請求註解 除了在@RequestMapping中,添加method請求的方法外,也可以直接將@RequestMapping改成對應的請求方式的註解 @GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping ```java= @GetMapping("/add/{a}/{b}") public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果為" + res); return "test"; } } ``` ![](https://i.imgur.com/8RvOd5X.png) #### 測試:寫兩個不同請求,相同路徑的方法 ```java= @PostMapping("/add/{a}/{b}") public String test1(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果1為" + res); return "test"; } @GetMapping("/add/{a}/{b}") public String test2(@PathVariable int a,@PathVariable int b, Model model){ int res = a + b; model.addAttribute("msg","結果2為" + res); return "test"; } ``` 得到的是GET請求的結果 ![](https://i.imgur.com/R5gBxwe.png) ### 嘗試用表單來執行POST請求 #### 1.先寫個a.jsp頁面 表示走add路徑並且執行post請求 ```xml= <html> <head> <title>Title</title> </head> <body> <form action="/add/" method="post"> <input type="text" name="a"> <input type="text" name="b"> <input type="submit"> </form> </body> </html> ``` 2.直接輸入路徑會失敗 ![](https://i.imgur.com/NU1JXV0.png) 3.需要先在web.xml註冊這個頁面 ```xml= <servlet> <servlet-name>a</servlet-name> <jsp-file>/WEB-INF/jsp/a.jsp</jsp-file> </servlet> <servlet-mapping> <servlet-name>a</servlet-name> <url-pattern>/a</url-pattern> </servlet-mapping> ``` 成功出現 ![](https://i.imgur.com/oPNIFWB.png) 4.輸入數字提交後,報404異常(錯誤原因不明) ![](https://i.imgur.com/zOMKEww.png) 5.重新編寫a.jsp,把路徑寫死 ```xml= <form action="/add/1/2" method="post"> <input type="submit"> ``` 成功,網址輸入http://localhost:8080/a 進入 ![](https://i.imgur.com/LA32aut.png) 送出查詢後,網址變成http://localhost:8080/add/1/2 與前面相同,但卻是執行POST請求 ![](https://i.imgur.com/xzc49rn.png)