Springboot 2.x 请求参数之 @CookieValue 使用

一、@CookieValue 作用

使用该注解可以获取指定名称的 Cookie ,如果你想获取更多的 Cookie 信息,可以使用 javax.servlet.http.Cookie 来定义形参类型

 

二、@CookieValue 注解声明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @CookieValue 获取指定的 HTTP cookie
/**
 * Annotation which indicates that a method parameter should be bound to an HTTP cookie.
 *
 /*
 // 也可以将形式参数的类型设置为 javax.servlet.http.Cookie 类型,然后调用相应的方法来获取 Cookie 信息
 /**
 * <p>The method parameter may be declared as type {@link javax.servlet.http.Cookie}
 * or as cookie value type (String, int, etc.).
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 3.0
 * @see RequestMapping
 * @see RequestParame
 * @see RequestHeader
 * @see org.springframework.web.bind.annotation.RequestMapping
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CookieValue {
 
    // name 和 value 互为别名
    /**
     * Alias for {@link #name}.
     */
    @AliasFor("name")
    String value() default "";
 
    /**
     * The name of the cookie to bind to.
     * @since 4.2
     */
    @AliasFor("value")
    String name() default "";
 
    // required 默认值为 true ,如果缺少指定名称的 cookie ,则会报错
    // 将 required 的值设置为 false ,即使 缺少指定名称的 cookie ,也不会报错
    /**
     * Whether the cookie is required.
     * <p>Defaults to {@code true}, leading to an exception being thrown
     * if the cookie is missing in the request. Switch this to
     * {@code false} if you prefer a {@code null} value if the cookie is
     * not present in the request.
     * <p>Alternatively, provide a {@link #defaultValue}, which implicitly
     * sets this flag to {@code false}.
     */
    boolean required() default true;
 
    // required 的值为 true 时,当缺少指定名称的 cookie ,为了不报错,可以使用 defaultValue 给这个 cookie 设置默认值
    /**
     * The default value to use as a fallback.
     * <p>Supplying a default value implicitly sets {@link #required} to
     * {@code false}.
     */
    String defaultValue() default ValueConstants.DEFAULT_NONE;
}

  

三、@CookieValue 使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@RestController
public class RequestParamsController {
 
    @GetMapping("/cookieParams")
    public Map userInfo(
            // 根据指定的 cookie 的 name 获取 value
            @CookieValue("Cookie_001") String cookie001,
            // 形参声明为 javax.servlet.http.Cookie ,会将 cookie 的 value 该类中
            @CookieValue("Cookie_002") Cookie cookie){
 
        // 获取 Cookie_002 的 name 和 value
        String name = cookie.getName();
        String value = cookie.getValue();
 
        Map map = new HashMap<String, Object>();
        map.put("Cookie_001",cookie001);
        map.put("name",name);
        map.put("value",value);
 
        return map;
    }
}

  

四、测试结果

4.1、请求携带的 Cookie 信息

4.2、响应信息

 

posted @   变体精灵  阅读(1217)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示