@CookieValue
@CookieValue
您可以使用 @CookieValue
注解将 HTTP cookie 的值绑定到控制器中的方法参数。
考虑一个带有以下 cookie 的请求:
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
以下示例展示了如何获取 cookie 值:
- Java
- Kotlin
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) { 1
//...
}
获取
JSESSIONID
cookie 的值。
@GetMapping("/demo")
fun handle(@CookieValue("JSESSIONID") cookie: String) { 1
//...
}
获取
JSESSIONID
cookie 的值。
如果目标方法参数类型不是 String
,则会自动应用类型转换。请参见 Type Conversion。