@RequestParam
主要用于接收url?后面的参数,get或post请求,只要后面的url?有参数都可以获取到对应的参数
@RequestParam
注解有几个比较重要的属性required 表示是否必须,默认为 true,必须。
defaultValue 可设置请求参数的默认值。
value 为接收url的参数名(相当于key值)。
对于对象也能够进行默认接收,前提是参数要与对象属性一致才能够进行注入
@GetMapping("/requestParam")
@ResponseBody
public Map<String, String> requestParam(
UserDto userDto,//通过一个实体类来接收,字段名必须一致
@RequestParam(value = "id", required = false) String userId,
@RequestParam(value = "name", required = false) String userName,
@RequestParam(value = "pageIndex", required = true, defaultValue = "1") String pageIndex,
@RequestParam(value = "pageSize", required = true, defaultValue = "5") String pageSize) {
Map<String, String> map = new HashMap<>();
map.put("userDto",userDto.toString());
map.put("id", userId);
map.put("name", userName);
map.put("pageIndex", pageIndex);
map.put("pageSize", pageSize);
return map;
}
无参数请求
{
"pageIndex": "1",
"name": null,
"pageSize": "5",
"id": null,
"userDto": "UserDto{userName='null', password='null'}"
}
GET http://localhost:8080/requestParam?name=test1&id=2233&userName=nongcha&password=123456
{
"pageIndex": "1",
"name": "test1",
"pageSize": "5",
"id": "2233",
"userDto": "UserDto{userName='nongcha', password='123456'}"
}