zoukankan      html  css  js  c++  java
  • Springboot 2.x 请求参数之 @CookieValue 使用

    一、@CookieValue 作用

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

    二、@CookieValue 注解声明

    // @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 使用

    @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、响应信息

  • 相关阅读:
    UVA 10828
    素数推断算法(高效率)
    POJ1611 The Suspects (并查集)
    Android AES加密算法及事实上现
    C语言中的内存对齐
    OpenCV——老照片效果
    uuid 学习
    OpenCV——PS 滤镜, 浮雕效果
    加密算法之BLOWFISH算法
    OpenCV——照亮边缘
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/14289804.html
Copyright © 2011-2022 走看看