SpringMVC study notes (four) --- RestFul style

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

Operation using RESTful resources : different effects may be achieved by different request! As follows: request the same address, but the function can be different!

Learning test

  1. In the new category RestFulController

    @Controller
    public class RestFulController {
    }
    
  2. 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";
            
        }
        
    }
    
  3. Let's see the next test request
    1570172985792.png

    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.

    1570173040458.png

  4. 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";
    
    }
    

    1570173149326.png

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:
    1570173341179.png

  • 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";
    }
    

    1570173388674.png

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!

Published 16 original articles · won praise 11 · views 1054

Guess you like

Origin blog.csdn.net/qq_42295733/article/details/104062771