对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

@CookieValue

您可以使用@CookieValue注解将 HTTP Cookie 的值绑定到方法参数 在控制器中。spring-doc.cadn.net.cn

以下示例显示了一个带有 Cookie 的请求:spring-doc.cadn.net.cn

JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84

以下代码示例演示如何获取 Cookie 值:spring-doc.cadn.net.cn

@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) { (1)
	//...
}
1 获取 cookie 值。
@GetMapping("/demo")
fun handle(@CookieValue("JSESSIONID") cookie: String) { (1)
	//...
}
1 获取 cookie 值。

如果目标方法参数 type 不是String.请参阅类型转换spring-doc.cadn.net.cn