4. RestFul style
concept
Restful style is a resource location and resource operations. Not standard protocol is not just a style. Based on this style of software can be more concise, more structured and easier to implement caching mechanisms.
Features
- Resources: All the things that the Internet can be abstracted as resources
- Resources: Use POST, DELETE, PUT, GET, using different methods to manipulate resources.
- Respectively add, delete, modify, query.
The traditional way of operating resources : to achieve different effects by different parameters! A single method, post and get
- http://127.0.0.1/item/queryItem.action?id=1 query, GET
- http://127.0.0.1/item/saveItem.action new, POST
- http://127.0.0.1/item/updateItem.action update, POST
- http://127.0.0.1/item/deleteItem.action?id=1 delete, GET or POST
Operation using RESTful resources : different effects may be achieved by different request! As follows: request the same address, but the function can be different!
- http://127.0.0.1/item/1 query, GET
- http://127.0.0.1/item new, POST
- http://127.0.0.1/item update, PUT
- http://127.0.0.1/item/1 delete, DELETE
Learning test
-
In the new category RestFulController
@Controller public class RestFulController { }
-
You can use @PathVariable annotations in Spring MVC, so that the method parameter values corresponding bound to a URI template variables.
@Controller public class RestFulController { //映射访问路径 @RequestMapping("/commit/{p1}/{p2}") public String index(@PathVariable int p1, @PathVariable int p2, Model model){ int result = p1+p2; //Spring MVC会自动实例化一个Model对象用于向视图中传值 model.addAttribute("msg", "结果:"+result); //返回视图位置 return "test"; } }
-
Let's see the next test request
Reflections: The benefits of using path variables?
- The path becomes more concise;
- Get more convenient parameters, the framework will automatically type conversion.
- The path can be restrained by the type of variable access parameters, if not the same type, not corresponding to the access request method, as described herein is the access path / commit / 1 / a, the method does not match the path, rather than a parameter conversion failed.
-
Let's change under the corresponding parameter type, test again
//映射访问路径 @RequestMapping("/commit/{p1}/{p2}") public String index(@PathVariable int p1, @PathVariable String p2, Model model){ String result = p1+p2; //Spring MVC会自动实例化一个Model对象用于向视图中传值 model.addAttribute("msg", "结果:"+result); //返回视图位置 return "test"; }
Using the method attribute specifies the request type
Type constraint for requested, the request may be narrow range. The request specifies the type of verbs GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE, etc.
Let's test:
-
One way to increase
//映射访问路径,必须是POST请求 @RequestMapping(value = "/hello",method = {RequestMethod.POST}) public String index2(Model model){ model.addAttribute("msg", "hello!"); return "test"; }
-
We use the browser address bar to access the default Get request, the error 405:
-
If you modify the GET POST is normal;
//映射访问路径,必须是Get请求 @RequestMapping(value = "/hello",method = {RequestMethod.GET}) public String index2(Model model){ model.addAttribute("msg", "hello!"); return "test"; }
summary:
Spring MVC annotation of @RequestMapping processing method capable of HTTP requests, such as GET, PUT, POST, DELETE, and PATCH.
All the address bar will be the default HTTP GET request type.
Level annotations method has the following variants: a combination of notes
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
@GetMapping is a combination comment
It is played by a shortcut @RequestMapping (method = RequestMethod.GET) of.
Usually will use more of!