java – get cookie value by name in spring mvc

The simplest way is using it in a controller with the @CookieValue annotation:

@RequestMapping("/hello")
public String hello(@CookieValue("foo") String fooCookie) {
    // ...
}

Otherwise, you can get it from the servlet request using Spring org.springframework.web.util.WebUtils

WebUtils.getCookie(HttpServletRequest request, String cookieName)

By the way, the code pasted into the question could be refined a bit. Instead of using #setHeader(), this is much more elegant:

response.addCookie(new Cookie("test", "value"));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)