@CookieValue
你可以使用 @CookieValue
註解將 HTTP cookie 的值繫結到控制器中的方法引數。
考慮一個帶有以下 cookie 的請求
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
以下示例展示瞭如何獲取 cookie 值
-
Java
-
Kotlin
@GetMapping("/demo")
public void handle(@CookieValue("JSESSIONID") String cookie) { (1)
//...
}
1 | 獲取 JSESSIONID cookie 的值。 |
@GetMapping("/demo")
fun handle(@CookieValue("JSESSIONID") cookie: String) { (1)
//...
}
1 | 獲取 JSESSIONID cookie 的值。 |
如果目標方法引數型別不是 String
,將自動應用型別轉換。請參閱型別轉換。