When a browser sends a request, it first goes through the DispatcherServlet, which forwards it to the various *Controller programs for processing. After processing, the result is returned to the DispatcherServlet, and then sent back to the browser.
The DispatcherServlet is known as the core controller or front controller.
Browser request data is encapsulated into an HttpServletRequest object, and response data into an HttpServletResponse object.
*Controllers receive requests and set responses.
Request
Simple Parameters
First, the traditional way: get request parameters manually via the HttpServletRequest object.
| |
When you visit http://localhost:8080/simpleParam?name=tom&age=18 in your browser, the page returns “OK”, and the program outputs “Tom;18”.
Clearly, this method is too cumbersome. In Spring Boot, you can simply define method parameters with the same names as the request parameters to receive them.
| |
Same effect as above.
However, the above method will still run even if the request parameter names differ; missing parameters will be null.
You can use @RequestParam to map request parameter names to different method parameter names.
| |
Object Parameters
For a few parameters, the above methods work. But for more data, you can use a simple object (POJO). If request parameter names match the POJO’s property names, just define a POJO to receive them.
| |
Visiting http://localhost:8080/simplePojo?name=tom&age=18 returns “OK”, and the program outputs “User{name=‘tom’, age=18}”.
Complex object parameters: If request parameter names match the method parameter’s object property names, nested POJO properties can be received by following the object hierarchy.
| |
Visiting http://localhost:8080/complexPojo?name=tom&age=18&address.province=beijing&address.city=beijing returns “OK”, and the program outputs “UserComplex{name=‘tom’, age=1000, address=Address{province=‘ads,beijing’, city=‘asda,beijing’}}”.
Array Parameters
If the request parameter name matches the method’s array parameter name, and there are multiple values, you can define an array-type method parameter to accept them. For example, for checkboxes.
| |
Visiting http://localhost:8080/arrayParam?hobby=ads&hobby=58s returns “OK”, and the program outputs “[ads, 58s]”.
Collection Parameters
If the request parameter name matches the method’s collection parameter name, and there are multiple values, use @RequestParam to bind the parameters.
| |
Visiting http://localhost:8080/listParam?hobby=ads&hobby=58s returns “OK”, and the program outputs “[ads, 58s]”.
Date Parameters
Use the @DateTimeFormat annotation to convert date parameter formats.
It’s worth noting there are three common parameter formats:
- yyyy-MM-dd HH:mm:ss
- yyyy/MM/dd HH:mm:ss
- yyyy年MM月dd日 HH时mm分ss秒 (yyyy年MM月dd日 HH时mm分ss秒)
Frontend and backend need to agree on the format.
| |
Visiting http://localhost:8080/dateParam?localDateTime=2024-01-01%2001:01:01 returns “OK”, and the program outputs “2024-01-01T01:01:01”.
JSON Parameters
If JSON data keys match the method parameter’s object property names, you can define a POJO-type method parameter to receive them. You’ll need to use @RequestBody.
To send JSON data, you must use a POST request.
| |
Request data:
| |
Using POST to request http://localhost:8080/jsonParam with the data above, the page returns “OK”, and the program outputs “UserComplex{name=‘jane’, age=18, address=Address{province=‘上海’, city=‘上海’}}”.
Path Parameters
Pass parameters directly in the request URL. Use {...} to mark path parameters, and @PathVariable to retrieve them.
| |
Visiting http://localhost:8080/path/52 returns “OK”, and the program outputs “52”.
Visiting http://localhost:8080/path/255 returns “OK”, and the program outputs “255”.
For multiple path parameters:
| |
Visiting http://localhost:8080/path/255/Tom returns “OK”, and the program outputs “255;Tom”.
Summary
- Simple Parameters
- Define method parameters; request parameter names should match variable names.
- If they don’t match, map them manually with
@RequestParam.
- Object Parameters
- Request parameter names should match object property names for automatic encapsulation.
- Array & Collection Parameters
- Arrays: Request parameter name matches array name for direct encapsulation.
- Collections: Request parameter name matches collection name; bind with
@RequestParam.
- Date Parameters
@DateTimeFormat
- JSON Parameters
@RequestBody
- Path Parameters
@PathVariable
Response
Use @ResponseBody to handle responses.
The above request methods could send responses because the
@RestControllerannotation includes both@Controllerand@ResponseBody.
@ResponseBody
Type: Method annotation, Class annotation
Location: On a Controller method or class
Purpose: Returns the method’s return value directly as the response. If the return type is an object or collection, it will be converted to JSON format.
| |
Visit the respective URLs to test.
Unified Response Result
The return types of the three methods above are all different, which makes frontend development harder. To address this, you can unify the response results into a single class.
| |
Making all response methods return a Result object makes project management and maintenance easier.
For example, this class:
| |
Then the above code can be changed to:
| |
Visit the respective URLs to test.