Learn SpringBoot: how many parameters can you get through a request?

Previously, the principle of controller layer parameter encapsulation in springboot has been analyzed in the spring boot source code analysis - controller layer parameter encapsulation, but there will not be time for you to analyze slowly in the work, and sometimes it is necessary to query quickly. So today I'll summarize which parameters of the controller layer encapsulate the meaning and use of annotations, so as to facilitate quick reference in the future.

@RequestParam annotation

There are two main uses of this annotation. Examples are as follows

1. Specify 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 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 the get request values, we can not explicitly specify the value of the RequestParam annotation, and use the map to receive the parameters. In this way, we can get all the requested values.

@PathVariable annotation

There are two main uses of this annotation. Examples are as follows

1. Specify 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 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 annotation

If you want to use this annotation, you need to turn on the configuration

@Component
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

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

}

There are two main usage examples of this annotation

Specify 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 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 annotation

The post request is encapsulated into an entity bean

post Request body
{
	"name":"liu",
	"id":"123"
}

    @PostMapping("hello")
    public String hello(@RequestBody User user){
        System.out.println(user);
        return "hello";
    }

    user(id=123, name=liu)

@RequestHeader annotation

Get the field of the request header. Similarly, there are two ways to get a single request header and all request headers.

@CookieValue annotation

Gets the value of the key value pair in the cookie.

Request header addition Cookie:value=liuyu
    @GetMapping("hello")
    public String hello(@CookieValue(name = "value") String user){
        System.out.println(user);
        return "hello";
    }
    user = liuyu

@SessionAttribute annotation and @ RequestAttribute annotation

The two annotation functions are a bit similar. One is to find the corresponding object in the real session, and the other is to find the corresponding object in the request.

Author: I'm general Xing Daorong
Link: https://juejin.cn/post/6844903873182711816

last

As a person from the past, Xiaobian has sorted out a lot of advanced architecture video materials, interview documents and PDF learning materials. For the above set of system outline, Xiaobian also has corresponding advanced architecture video materials. If * * 'you' determine your future path or want to learn and improve your technology stack and technical knowledge, you can Click here to get free learning materials Improve yourself (full set of interview documents, PDF, advanced architecture video)**


To get free learning materials]( https://docs.qq.com/doc/DSmxTbFJ1cmN1R2dB )Improve yourself (full set of interview documents, PDF, advanced architecture video)**

[external chain picture transferring... (img-jGOtBM9C-1623618358850)]
[external chain picture transferring... (img-AvEQnb6w-1623618358851)]

Keywords: Java Interview Programmer

Added by dud3r on Sun, 30 Jan 2022 00:11:53 +0200