1. 普通传参
@RequestMapping(path = "/{city_id}/{user_id}", method = RequestMethod.GET)
public Object findUser(@PathVariable("city_id") String cityId,
@PathVariable("user_id") String userId) {
params.clear();
params.put("cityId", cityId);
params.put("userId", userId);
return params;
}
2. GetMapping注解
@GetMapping(value = "/v1/page_user1")
public Object pageUser(int from, int size) {
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
3. 默认值,参数是否必须
@GetMapping(value = "/v1/page_user2")
public Object pageUserV2(@RequestParam(defaultValue = "0", name = "page", required = true) int from, int size) {
params.clear();
params.put("from", from);
params.put("size", size);
return params;
}
4. bean对象传参
@GetMapping("/v1/save_user")
public Object saveUser(@RequestBody User user) {
params.clear();
params.put("user", user);
return params;
}
注意:
- 需要指定 http content-type 为 application/json
- 使用body传输数据(针对postman而言)
5. 获取http头信息
@GetMapping("/v1/get_header")
public Object getHeader(@RequestHeader("access_token") String accessToken, String id) {
params.clear();
params.put("access_token", accessToken);
params.put("id", id);
return params;
}
6. HttpServletRequest
@GetMapping("/v1/test_request")
public Object testRequest(HttpServletRequest request) {
params.clear();
String id = request.getParameter("id");
params.put("id", id);
return params;
}