SpringBoot learning - how many parameters can be obtained through a request

Before parsing SpringBoot source package -controller layer parameters have been analyzed principle controller layer parameters springboot package, but it does not work, after all, you always have time to analyze slowly, sometimes quickly query is also very necessary. So today to sum up the meaning of which parameters package annotated controller layer, and use, convenient quick reference later.

@RequestParam comment

The annotation is mainly used in two ways, as an example

1. Specify the parameter name

    http://127.0.0.1:8080/hello?id=123
    
    @GetMapping("hello")
    public String hello(@RequestParam("id") String id){
        log.info("[{}]",id);
        return "hello";
    }
    
    id = 123
复制代码

2. do not specify the parameter name

    http://127.0.0.1:8080/hello?id=123&name=liuyu
    
    @GetMapping("hello")
    public String hello(@RequestParam Map<String,String> value){
        System.out.println(value);
        return "hello";
    }
    value = {id=123, name=liuyu}
复制代码

If we want to get all of the requested value get value, you can not explicitly specify RequestParam annotations, and use the map to receive parameters. So that we can get all of the requested value.

@PathVariable comment

The annotation is mainly used in two ways, as an example

1. Specify the parameter name

    http://127.0.0.1:8080/hello/liuyu/qwert
    
    @GetMapping("hello/{id}/{name}")
    public String hello(@PathVariable("id") String id,@PathVariable("name") String name){
        System.out.println(value);
        System.out.println(name);
        return "hello";
    }
    id = liuyu ,name = qwert
复制代码

2. do not specify the parameter name

    http://127.0.0.1:8080/hello/liuyu/qwert

    @GetMapping("hello/{id}/{name}")
    public String hello(@PathVariable Map<String,String> map){
        System.out.println(map);
        return "hello";
    }
    
    map = {id=liuyu, name=qwert}
复制代码

@MatrixVariable comment

If you want to use this annotation need to open the configuration

@Component
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper=new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        configurer.setUrlPathHelper(urlPathHelper);
    }

}
复制代码

The annotation is mainly used in two ways, as an example

Specified parameter name

   http://127.0.0.1:8080/hello/liu;value=123
    
   @GetMapping("hello/{id}")
    public String hello(@PathVariable(name = "id") String name,@MatrixVariable(name = "value") String value){
        System.out.println(name);
        System.out.println(value);
        return "hello";
    }
    id = liu
    value = 123
复制代码

Do not specify the parameter name

    http://127.0.0.1:8080/hello/liu;value=123;name=qwe

    @GetMapping("hello/{id}")
    public String hello(@PathVariable(name = "id") String name,@MatrixVariable Map<String,String> value){
        System.out.println(name);
        System.out.println(value);
        return "hello";
    }
    id = liu
    value = {value=123, name=qwe}
复制代码

@RequestBody comment

post request, the request encapsulated into the specific entity bean

post请求体
{
	"name":"liu",
	"id":"123"
}

    @PostMapping("hello")
    public String hello(@RequestBody User user){
        System.out.println(user);
        return "hello";
    }
    
    user(id=123, name=liu)
复制代码

@RequestHeader comment

Acquiring request header fields, there are the same two uses of a single acquisition and capture all request header request header.

@CookieValue comment

Gets the value of key-value pairs in the cookie.

    请求头添加 Cookie:value=liuyu
    @GetMapping("hello")
    public String hello(@CookieValue(name = "value") String user){
        System.out.println(user);
        return "hello";
    }
    user = liuyu
复制代码

@SessionAttribute annotations and notes @RequestAttribute

Both annotation functions a bit like a real session to find the corresponding object, one is looking at the request of.


Back to Contents

Guess you like

Origin juejin.im/post/5d0d9379e51d455c8838e17b