The common ways a Controller receives parameters can be divided into three categories. The first type is Get request through concatenated URL, the second type is Post request through request body, and the third type is parameter through request header.

1 @pathvariable Receives parameters

Request mode: localhost:7001/param/123

Example request:

Code examples:

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

2 @requestParam receives parameters

The @requestParam annotation can specify a name. The request parameter must be the same as the specified name. If it is not specified, it defaults to a specific parameter name.

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

Example request:

Code examples:

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

3 Send parameters without comments

The main difference between this method and 2 is that this parameter is not mandatory and may not be passed on the request path.

Request: localhost: 7001 / param/get string? id=18

Example request:

Code examples:

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

4 HttpServletRequest receives parameters

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

Example request:

Code examples:

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

5 @requestbody receives the RequestBody parameter

This method is usually used to pass entity objects, with this annotation, the parameters are also mandatory.

{“id”:18}

Example request:

Code examples:

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

6 @requestheader receives the RequestHeader parameters

Example request:

Code examples:

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

The sample code for this article has been uploaded togithub, point astarSupport!

Spring Boot series tutorial directory

Spring-boot-route (I) Several ways for Controller to receive parameters

Spring-boot-route (2) Several methods of reading configuration files

Spring-boot-route (3) Upload multiple files

Spring-boot-route (4) Global exception processing

Spring-boot-route (5) Integrate Swagger to generate interface documents

Spring-boot-route (6) Integrate JApiDocs to generate interface documents

Spring-boot-route (7) Integrate jdbcTemplate operation database

Spring-boot-route (8) Integrating mybatis operation database

Spring-boot-route (9) Integrate JPA operation database

Spring-boot-route (10) Switching between multiple data sources

Spring-boot-route (11) Encrypting database configuration information

Spring-boot-route (12) Integrate REDis as cache

Spring-boot-route RabbitMQ

Spring-boot-route Kafka

Spring-boot-route (15) Integrate RocketMQ

Spring-boot-route (16) Use logback to produce log files

Spring-boot-route (17) Use AOP to log operations

Spring-boot-route (18) Spring-boot-adtuator monitoring applications

Spring-boot-route (19) Spring-boot-admin Monitoring service

Spring-boot-route (20) Spring Task Implements simple scheduled tasks

Spring-boot-route (21) Quartz Implements dynamic scheduled tasks

Spring-boot-route (22) Enables email sending

Spring-boot-route (23) Developed wechat official accounts

Spring-boot-route (24) Distributed session consistency processing

Spring-boot-route (25) two lines of code to achieve internationalization

Spring-boot-route (26) Integrate webSocket

This series of articles are frequently used in the work of knowledge, after learning this series, to cope with daily development more than enough. If you want to know more, just scan the qr code below and let me know. I will further improve this series of articles!