In Spring Boot, there are many ways to receive request parameters, mainly the following ways:
1. Annotate with @RequestParam
@RequestParam is used to receive query parameters in the request. It is usually used to handle key-value pairs in URLs, such as ?key=value.
Example: Receive query parameters
@GetMapping("/example") public String example(@RequestParam String name, @RequestParam int age) { return "Name: " + name + ", Age: " + age; }
In this example, if the URL is /example?name=John&age=30, the values of name and age are passed to the method.
Parameter type conversion: Spring will automatically convert parameter types. If the types do not match (for example, failing to convert a string to an integer), an exception is thrown.
Default value: You can set the default value, and the default value will be used when there is no corresponding parameter in the request.
@GetMapping("/example") public String example(@RequestParam(defaultValue = "John") String name) { return "Name: " + name; }
2. Annotation with @PathVariable
@PathVariable is used to receive parameters in the path, usually used in RESTful-style URLs.
Example: Receive path parameters in URL
@GetMapping("/user/{id}") public String getUser(@PathVariable String id) { return "User ID: " + id; }
For example, when the request path is /user/123, the id parameter receives 123.
3. Annotation with @RequestBody
@RequestBody is used to receive data in the request body, usually used for POST requests or PUT requests. It can deserialize the request body's data into specified Java objects (such as JSON, XML, etc.).
Example: Receive request body in JSON format
@PostMapping("/createUser") public String createUser(@RequestBody User user) { return "User created: " + (); }
Assume that the request body sent is:
{ "name": "John", "age": 30 }
@RequestBody converts the JSON data of the request body into a User object.
4. Use @ModelAttribute annotation
@ModelAttribute is used to bind request parameters into JavaBeans for method parameters. It is often used for form submission or data passed using GET requests.
Example: Form data bound to Java object
@PostMapping("/submitForm") public String submitForm(@ModelAttribute User user) { return "Form submitted by: " + (); }
Suppose the data submitted by the HTML form is:
<form action="/submitForm" method="post"> <input type="text" name="name" /> <input type="number" name="age" /> </form>
Note: @ModelAttribute matches all parameters in the request with fields of a Java object.
5. Use @RequestHeader annotation
@RequestHeader is used to get the data in the request header. This annotation allows you to obtain some information about the HTTP request header.
Example: Receive request header
@GetMapping("/header") public String getHeader(@RequestHeader("User-Agent") String userAgent) { return "User-Agent: " + userAgent; }
Assuming that the request header contains the User-Agent field, @RequestHeader will pass its value to the userAgent variable.
6. Annotation with @CookieValue
@CookieValue is used to get the cookie value in the request.
Example: Receive cookies in request
@GetMapping("/cookie") public String getCookie(@CookieValue("JSESSIONID") String sessionId) { return "Session ID: " + sessionId; }
Assuming that the request contains a cookie named JSESSIONID, @CookieValue will pass its value to the sessionId.
7. Collection type using @RequestParam
@RequestParam also supports receiving parameters of array or collection types.
Example: Receive multiple parameters with the same name
@GetMapping("/items") public String getItems(@RequestParam List<String> items) { return "Items: " + items; }
For example, if you request /items?items=apple&items=banana&items=cherry, items will receive ["apple", "banana", "cherry"].
8. Annotation with @RequestPart
@RequestPart is used to process uploaded files or multipart form data (multipart/form-data). It is used to extract data from a file or other part of the data from a request.
Example: Receive files and other form fields
@PostMapping("/upload") public String uploadFile(@RequestPart("file") MultipartFile file, @RequestPart("description") String description) { return "File uploaded: " + () + ", Description: " + description; }
Here, file is the received file through the @RequestPart annotation, and description is the other fields received.
9. Params property annotated with @RequestMapping
The params property can be used in @RequestMapping to restrict methods to be called only if they satisfy a specific query parameter.
Example: The request must contain specific parameters before it can be executed
@RequestMapping(value = "/search", params = "query") public String search(@RequestParam String query) { return "Search query: " + query; }
This method will only be matched if the request path is /search?query=value.
Summarize
- @RequestParam: Used to query parameters.
- @PathVariable: Used for path parameters.
- @RequestBody: used in request body, often used to receive data in JSON, XML and other formats.
- @ModelAttribute: Used to bind request parameters into JavaBeans.
- @RequestHeader: Used to receive request header data.
- @CookieValue: Used to receive cookie values.
- @RequestPart: Used to handle uploaded file and form fields.
- @RequestParam of collection type: Used to receive multiple parameters with the same name.
- @RequestMapping params property: Used to restrict the reception of specific query parameters.
These methods can be selected and used according to different needs, and flexibly handle request parameters in Spring Boot.
The above is the detailed content of the commonly used request parameters for springboot. For more information on the receiving methods for springboot request parameters, please pay attention to my other related articles!