# How to read cookies in Spring Boot

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Sep 25, 2019, updated Sep 25, 2022. Topics: Spring Boot.
Canonical URL: https://attacomsian.com/blog/read-cookies-spring-boot

> A quick guide to learning how to use the `@CookieValue annotation to read cookies in Spring Boot.

There are three ways to read HTTP [cookies in Spring Boot](https://attacomsian.com/blog/cookies-spring-boot):

1. Use the `@CookieValue` annotation to read an individual cookie.
2. The `WebUtils` class provides utility methods to fetch information from the request object.
3. 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:

```java
@GetMapping("/profile")
public String profilePage(@CookieValue(name = "color") String color) {
    return "You are using " + color + " mode.";
}
```

The `@CookieValue` annotation takes several parameters:

* `name` &mdash; The name of the cookie to bind the method's parameter.
* `required` &mdash; Whether the cookie is required. Defaults to `true`, leading to an exception if the cookie is missing in the request. Switching this to `false` will set the cookie value to `null ` if the cookie is absent from the request.
* `defaultValue` &mdash; The default value to use as a fallback. Passing a default value will automatically set `required` 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:

```java
@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:

```java
@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](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/WebUtils.html) 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:

```java
@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.

```java
@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!";
}
```
