@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,則會自動套用類型轉換。請參閱類型轉換