@CookieValue

可以使用 @CookieValue 标注,将 HTTP cookie 的值绑定到 Controller 中的方法参数。

You can use the @CookieValue annotation to bind the value of an HTTP cookie to a method argument in a controller.

考虑带有以下 cookie 的请求:

Consider a request with the following cookie:

JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84

以下示例说明了如何获取 cookie 值:

The following example shows how to get the cookie value:

Java
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) { 1
	//...
}
1 Get the value of the JSESSIONID cookie.
Kotlin
@GetMapping("/demo")
fun handle(@CookieValue("JSESSIONID") cookie: String) { (1)
	//...
}
2 Get the value of the JSESSIONID cookie.

如果目标方法参数类型不是 String,则自动应用类型转换。参见 Type Conversion

If the target method parameter type is not String, type conversion is applied automatically. See Type Conversion.