Spring Boot Request Parameter Receive Control Guide
1. Overview
In Spring Boot project, we can receive various parameters from HTTP requests through different annotations.
This guide will introduce in detail the various parameters receiving methods and usage scenarios.
2. Comparison of parameter reception methods when there are comments
Parameter Type | annotation | Location | Applicable HTTP method | Example |
---|---|---|---|---|
Path parameters | @PathVariable | In the URL path | GET, DELETE | /users/{id} |
Query parameters | @RequestParam | URL? | GET | /users?name=John |
Request body | @RequestBody | Request body | POST, PUT, PATCH | JSON/XML data |
Request header | @RequestHeader | HTTP header | Any | Authorization: Bearer token |
Cookie value | @CookieValue | Cookie header | Any | Cookie: JSESSIONID=xxx |
Form data | @RequestParam | Form body | POST | application/x-www-form-urlencoded |
File upload | @RequestParam | multipart/form-data | POST | File upload form |
3. The default location of the receiving parameters when there is no annotation
Spring Boot default parameter binding rules (when not annotated)
Request Type | Parameter Type | Default binding location | Example | Things to note |
---|---|---|---|---|
GET | Basic Type/String | Automatically bind to URL query parameters | GET /user?name=Tom → public String getUser(String name) | The parameter name must be consistent with the key in the URL |
POJO Object | Automatically bind to URL query parameters | GET /user?name=Tom&age=20 → public String getUser(User user) | The field name of the object must match the URL parameter | |
Array/collection | Automatically bind to URL query parameters | GET /user?ids=1,2,3 → public String getUser(List<Integer> ids) | Support comma-separated or parameters of the same name (ids=1&ids=2) | |
POST | Basic Type/String | Form Data | POST /user (Content-Type: application/x-www-form-urlencoded) | Content-Type needs to be set as form type |
POJO Object | Form data or JSON request body | If it is application/json → bind to the request body; if it is form → match by field name | Content-Type needs to be specified explicitly, otherwise the parsing may fail | |
Array/collection | Form data or JSON request body | Same as POJO rules | The form format requires the same name parameter (names=Tom&names=Jerry) | |
MultipartFile | File upload (multipart) | POST /upload (enctype="multipart/form-data") | Enctype="multipart/form-data" must be set | |
PUT/PATCH | All types | JSON/XML request body | PUT /user { "name": "Tom" } → public String updateUser(User user) | By default, it is processed by @RequestBody, and explicit annotation is required to overwrite it. |
DELETE | Basic Type/String | URL query parameters | DELETE /user?id=123 → public String deleteUser(Long id) | Same as GET |
POJO Object | Not supported | - | DELETE usually only uses path parameters or simple query parameters |
Key Rules Summary
GET/DELETE request:
- Parameters are bound to defaultURL query string(
?key=value
)。 - The POJO object will be automatically disassembled to the query parameters according to the field name.
POST request:
- By default, pressForm data(
application/x-www-form-urlencoded
) Analysis. - If the request header is
Content-Type: application/json
, it needs to be explicitly added@RequestBody
, otherwise the binding fails.
PUT/PATCH Request:
-
By default, processed by request body (JSON/XML),similar
@RequestBody
behavior, but it is recommended to explicitly add comments in actual development.
Special types:
-
MultipartFile
: Onlymultipart/form-data
Automatic binding in POST type. - Array/collection: Supports URL query parameters (GET) or form with the same name (POST).
Common misunderstandings clarification
The form data will not be placed in the request header
- Request headers are only used for metadata (e.g.
Content-Type
、Authorization
etc.), the content of the form must be in the request body.
Can GET requests bring form data?
- ❌ No. The parameters of the GET request can only be queryed through the URL (
?key=value
) passes, and the length is limited.
How to bind form data in Spring Boot without annotation?
- By default, the form fields in the request body are matched by parameter name (the parameter name must be consistent with the form field name).
POST JSON vs Form Data
- JSON data must be used explicitly
@RequestBody
, while form data is bound by default (no annotation required).
Request parameter location custom control comparison table
Parameter position | Spring Boot Annotations | Manual extraction method | Applicable scenarios | Sample Request |
---|---|---|---|---|
URL path parameters | @PathVariable | () | RESTful resource identification | GET /users/123 → id=123 |
URL query parameters | @RequestParam | ("name") | Filter conditions, paging | GET /users?name=Tom → name=Tom |
Request header | @RequestHeader | ("Authorization") | Authentication token, client information | Header: Authorization: Bearer xxx |
Request Body (JSON) | @RequestBody | InputStream + JSON parsing library | Complex data submission | POST /users + {"name":"Tom"} |
Request body (form) | @RequestParam | () | Traditional form submission | POST /login + username=Tom&password=123 |
Request body (file) | @RequestPart | MultipartHttpServletRequest | File upload | POST /upload + multipart/form-data |
Cookie | @CookieValue | () | Session Management | Cookie: sessionId=abc123 |
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.