spring-boot-route (1) Several ways for Controller to receive parameters

The common ways that Controller receives parameters can be generally divided into three categories. The first type is that Get requests are passed through splicing URLs, the second type is that Post requests are passed through the request body, and the third type is that parameters are passed through the request header.

1 @PathVariable receives parameters

Request method: localhost:7001/param/123

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@GetMapping("{id}")
public String getPathVariable(@PathVariable String id){
    return "id="+id;
}

2 @RequestParam receives parameters

There are two points to pay attention to when using this annotation. One is that this parameter must be passed in the request after adding this parameter. The second is that the @RequestParam annotation can specify a name, and the request parameter must be the same as the specified name. If not specified, the default is Is the specific parameter name.

Request method: localhost:7001/param/getParam?myId=18

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@GetMapping("getParam")
public String getRequestParam(@RequestParam("myId") String id){
    return "id="+id;
}

3 Passing parameters without annotations

Compared with 2 in this way, the biggest difference is that this parameter is not mandatory, and it is not necessary to pass it on the request path.

Request method: localhost:7001/param/getString?id=18

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@GetMapping("getString")
public String getString(String id){
    return "id="+id;
}

4 HttpServletRequest receiving parameters

Request method: localhost:7001/param/getRequest?id=18

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@GetMapping("getRequest")
public String getRequest(HttpServletRequest request){
    String id = request.getParameter("id");
    return "id="+id;
}

5 @RequestBody receives request body parameters

This method is generally used to transfer entity objects. After adding this annotation, the parameters must also be passed.

Request method: {"id":18}

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@PostMapping("getBody")
public String getBody(@RequestBody String id){
    return "id="+id;
}

6 @RequestHeader receives request header parameters

Example request:

spring-boot-route (1) Several ways for Controller to receive parameters

Code example:

@PostMapping("getHeader")
public String getHeader(@RequestHeader String id){
    return "id="+id;
}

This is the first article in the spring-boot-route series. The articles in this series are relatively simple. The main purpose is to help students who are new to Spring Boot have a systematic understanding. This article has been included in my github , welcome friends star!

githubhttps://github.com/binzh303/spring-boot-route

Pay attention, don't get lost

If you feel good essays, welcome attention , thumbs up , collection , your support is my creative power, thank you.

If there is a problem with the writing of the article, please don't be stingy. Please leave a message and point it out. I will check and modify it in time.

If you want to know me more deeply, you can search for " Java Journey " on WeChat to follow. Reply " 1024 " to get learning videos and exquisite e-books. Push technical articles on time at 7:30 every day, so that you are not alone on your way to work, and there are monthly book delivery activities to help you improve your hard power!

Guess you like

Origin blog.51cto.com/14820531/2539808