常用请求参数注解使用

路径变量@PathVariable

@RestController
public class ParameterTestController {

    /**
     * 测试 @PathVariable 注解
     */
    @GetMapping("/car/{carId}/owner/{userName}")
    public Map<String, Object> getCar(@PathVariable("carId") Integer carId,
                                      @PathVariable("userName") String userName,
                                      @PathVariable Map<String, String> allPathVariable) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("carId", carId);
        map.put("userName", userName);
        map.put("allPathVariable", allPathVariable);

        return map;
    }
}
  • 路径映射注解中用{}包裹的变量会与方法的参数中用了@PathVariable注解的变量对应,然后在方法中就可以直接去使用路径变量了

  • 查看@PathVariable注解的源码注释如下

    If the method parameter is Map<String, String> then the map is populated with all path variable names and values.
    

    也就是说,如果方法的参数中有一个Map<String, String>类型的参数的话,这个参数会被自动装配,内容就是所有的路径变量的key和value

访问结果

测试路径变量注解 -- @PathVariable

获取请求头@RequestHeader

/**
  * 测试 @RequestHeader 注解
*/
@GetMapping("/get-header")
public Map<String, Object> getHeaders(@RequestHeader("User-Agent") String userAgent,
                                      @RequestHeader MultiValueMap<String, String> multiValueMap,
                                      @RequestHeader HttpHeaders httpHeaders) {
    HashMap<String, Object> map = new HashMap<>();

    map.put("userAgent", userAgent);
    map.put("multiValueMap", multiValueMap);
    map.put("httpHeaders", httpHeaders);

    return map;
}
  • 类似地,该注解源码注释中有提到

    If the method parameter is Map<String, String>, MultiValueMap<String, String>, or HttpHeaders then the map is populated with all header names and values.
    

    所以这里测试的时候添加了三个参数,分别是Map<String, String>类型、MultiValueMap<String, String>类型和HttpHeaders类型的参数,然后返回给浏览器后查看是否真的自动装配了所有的headers

测试结果

测试获取请求头注解 -- @RequestHeader

获取请求参数@RequestParam

/**
 * 测试 @RequestParam 注解
 */
@GetMapping("/get-request-param")
public Map<String, Object> getRequestParam(@RequestParam("username") String userName,
                                           @RequestParam Map<String, String> commonMap,
                                           @RequestParam MultiValueMap<String, String> multiValueMap) {

    HashMap<String, Object> map = new HashMap<>();
    map.put("username", userName);
    map.put("commonMap", commonMap);
    map.put("multiValueMap", multiValueMap);

    return map;
}
  • 与前两个注解类似

    If the method parameter is Map<String, String> or MultiValueMap<String, String> and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.
    

测试结果

测试获取请求参数注解 -- @RequestParam

获取cookie值@CookieValue

/**
 * 测试 @CookieValue 注解
 */
@GetMapping("/get-cookie-value")
public Map<String, Object> getCookieValue(@CookieValue("csrftoken") String csrfToken,
                                          @CookieValue("csrftoken") Cookie csrfTokenCookie) {

    HashMap<String, Object> map = new HashMap<>();
    map.put("csrftoken-string", csrfToken);
    map.put("csrftoken-cookie-obj", csrfTokenCookie);
    String info = csrfTokenCookie.getName() + " ===> " + csrfTokenCookie.getValue();
    map.put("csrftoken-cookie-obj-info", info);

    return map;
}
  • 官方文档注释中提到

    The method parameter may be declared as type javax.servlet.http.Cookie or as cookie value type (String, int, etc.).
    

    也就是说参数中可以接收一个java原生的servlet Cookie对象

测试结果

测试获取cookie值注解 -- @CookieValue

获取请求体@RequestBody

  • 对于POSTPUTPATCH等有请求体的请求,要获取请求体的数据,就用该注解
/**
 * 测试 @RequestBody 注解
 */
@PostMapping("/post-request-body")
public Map<String, Object> getRequestBody(@RequestBody String requestBody) {

    HashMap<String, Object> map = new HashMap<>();
    map.put("requestBody", requestBody);

    return map;
}

测试结果

请求表单
请求结果

获取request请求域@RequestAttribute

  • 使用模板语言的时候才会用到这个,前后端分离的话就用不上
  • 可以获取原生HttpServletRequestsetAttribute方法设置的属性
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容