# Getting & Setting Cookie in Spring Boot ###### tags: `Spring Framework`、`Cookie` ## Get Cookie 使用 `@CookieValue("name")` 將 Cookie 裡面的值儲存到變數中。 ```kotlin @RequestMapping("/readCookie") fun readCookie(@CookieValue("name") value: String): String { return "" } ``` ## Set Cookie 建立 `Cookie` 物件並透過 `HttpServletResponse` 設定 Cookie。 > 透過 setMaxAge(int) 設定 Cookie 多少秒後失效。 ```kotlin @RequestMapping("/setCookie") fun setCookie(resp: HttpServletResponse): String{ val cookie = Cookie("name", "value") cookie.maxAge = 60 * 60 * 24 * 7 // 設定 Cookie 7 天後失效 resp.addCookie(cookie) return "" } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up