@RequestParam主要用于接收url?后面的参数,get或post请求,只要后面的url?有参数都可以获取到对应的参数

    @RequestParam注解有几个比较重要的属性
    required 表示是否必须,默认为 true,必须。
    defaultValue 可设置请求参数的默认值。
    value 为接收url的参数名(相当于key值)。

    对于对象也能够进行默认接收,前提是参数要与对象属性一致才能够进行注入

    1. @GetMapping("/requestParam")
    2. @ResponseBody
    3. public Map<String, String> requestParam(
    4. UserDto userDto,//通过一个实体类来接收,字段名必须一致
    5. @RequestParam(value = "id", required = false) String userId,
    6. @RequestParam(value = "name", required = false) String userName,
    7. @RequestParam(value = "pageIndex", required = true, defaultValue = "1") String pageIndex,
    8. @RequestParam(value = "pageSize", required = true, defaultValue = "5") String pageSize) {
    9. Map<String, String> map = new HashMap<>();
    10. map.put("userDto",userDto.toString());
    11. map.put("id", userId);
    12. map.put("name", userName);
    13. map.put("pageIndex", pageIndex);
    14. map.put("pageSize", pageSize);
    15. return map;
    16. }

    无参数请求

    1. {
    2. "pageIndex": "1",
    3. "name": null,
    4. "pageSize": "5",
    5. "id": null,
    6. "userDto": "UserDto{userName='null', password='null'}"
    7. }
    1. GET http://localhost:8080/requestParam?name=test1&id=2233&userName=nongcha&password=123456
    2. {
    3. "pageIndex": "1",
    4. "name": "test1",
    5. "pageSize": "5",
    6. "id": "2233",
    7. "userDto": "UserDto{userName='nongcha', password='123456'}"
    8. }