There are three ways to read HTTP cookies in Spring Boot:
- Use the
@CookieValue
annotation to read an individual cookie. - The
WebUtils
class provides utility methods to fetch information from the request object. - The
HttpServletRequest
class that retrieves all cookies as an array.
Using @CookieValue
Annotation
The simplest way to read a cookie value in Spring Boot is using the @CookieValue
annotation. It indicates that the controller's method parameter is bound to an HTTP cookie:
@GetMapping("/profile")
public String profilePage(@CookieValue(name = "color") String color) {
return "You are using " + color + " mode.";
}
The @CookieValue
annotation takes several parameters:
name
— The name of the cookie to bind the method's parameter.required
— Whether the cookie is required. Defaults totrue
, leading to an exception if the cookie is missing in the request. Switching this tofalse
will set the cookie value tonull
if the cookie is absent from the request.defaultValue
— The default value to use as a fallback. Passing a default value will automatically setrequired
to false.
In the example above, if a cookie with the name color is present in the HTTP request, the variable color
will contain its value. Otherwise, an exception will be thrown.
To avoid the runtime exception, use the defaultValue
parameter:
@GetMapping("/profile")
public String profilePage(@CookieValue(name = "color", defaultValue = "dark") String color) {
return "You are using " + color + " mode.";
}
You can also add multiple @CookieValue
annotations to bind more than one controller method parameter:
@GetMapping("/profile")
public String profilePage(@CookieValue(name = "name", defaultValue = "Atta") String name,
@CookieValue(name = "country", defaultValue = "PK") String country) {
return "I'm " + name + " from " + country;
}
Using WebUtils
Class
The WebUtils class provides built-in utilities for web applications to manipulate the incoming HTTP requests. Using this utility class, we can easily fetch information from the request object.
The getCookie()
method from the WebUtils
class returns the first cookie with the given name. Otherwise, a null
value is returned.
Here is an example:
@GetMapping("/profile")
public String profilePage(HttpServletRequest request) {
Cookie name = WebUtils.getCookie(request, "name");
if (name != null) {
return "My name is " + name.getValue();
} else {
return "Not found!";
}
}
Using HttpServletRequest
Class
The HttpServletRequest
class provides request information for HTTP servlets. You can call the getCookies()
method on its object to retrieve an array of Cookie
objects that the client sent with this request. This method returns null
if no cookies were sent.
@GetMapping("/preferences")
public String preferencesPage(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();
if (cookies != null) {
return Arrays.stream(cookies)
.map(c -> c.getName() + "=" + c.getValue())
.collect(Collectors.joining(", "));
}
return "No preferences found!";
}
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.