Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this article, we’ll learn how to combine Spring Session with Spring WebFlux. Specifically, we’ll learn how to use Spring WebSession, which unites Spring Session with Spring Boot 2’s WebFlux.

A Spring Session is defined as “a simplified Map of name-value pairs”. Sessions track values that are important to an HTTP session, like Users and Principals. Thus, we can use Session management along with the new reactive WebFlux Mono and Flux objects. Spring Session also supports using different application containers (rather than only Tomcat).

For more about Spring Session, check out another great article here on Baeldung.

2. Maven Setup

Now, let’s get our app set up and configured. Thankfully, configuring our pom.xml is pretty easy to do. First, we need to use Spring Boot 3.x.x along with the relevant Spring Session dependencies. Add the newest version through Maven Repository:

Then, we add them to pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    <version>3.1.5</version>
</dependency>
<dependency> 
    <groupId>org.springframework.session</groupId> 
    <artifactId>spring-session-core</artifactId> 
    <version>3.1.5</version> 
</dependency>

Those two dependencies are the minimum requirements for in-memory Session management.  For Redis, use:

Then, add the following to the pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>3.1.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>3.1.5</version>
 </dependency>

Now, let’s configure our classes.

3. In-Memory Configuration

To use in-memory configuration, add the config class:

@Configuration
@EnableSpringWebSession
public class SessionConfig {
 
    @Bean
    public ReactiveSessionRepository reactiveSessionRepository() {
        return new ReactiveMapSessionRepository(new ConcurrentHashMap<>());
    }
}

That associates a (reactive) repository with your session manager. It’ll store those values in a HashMap.

Importantly, the configuration class must include the @EnableSpringWebSession annotation.

4. Redis Configuration

Now, let’s hook up Redis. To use Redis to manage WebSessions, add the configuration class:

@Configuration
@EnableRedisWebSession
public class RedisConfig {
 
    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }
}

Note that the configuration class must include the @EnableRedisWebSession annotation. Remember, we can’t use the@EnableRedisWebSession and EnableSpringWebSession annotations together without causing an exception.

Docker is one of the easiest ways to interact with Redis. After installing Docker, we only need to enter three commands to do so. Run the command to bring up a Redis instance:

$ docker stop redis
$ docker rm redis
$ docker run -d --name redis -p 6379:6379 redis:4.0.5-alpine

Next, let’s test our app.

5. In Practice

Now, let’s add a reactive REST controller to our app:

@GetMapping("/websession")
public Mono getSession(WebSession session) {

    session.getAttributes().putIfAbsent("id", 0);
    session.getAttributes().putIfAbsent("note", "Howdy Cosmic Spheroid!");

    CustomResponse r = new CustomResponse();
    r.setId((int) session.getAttributes().get("id"));
    r.setNote((String) session.getAttributes().get("note"));

    return Mono.just(r);
}

Then, we can use WebSession by adding a parameter to our REST handlers:

@GetMapping("/websession/test")
public Mono<CustomResponse> testWebSessionByParam(@RequestParam(value = "id") int id,
  @RequestParam(value = "note") String note, WebSession session) {

    session.getAttributes().put("id", id);
    session.getAttributes().put("note", note);

    CustomResponse r = new CustomResponse();
    r.setId((int) session.getAttributes().get("id"));
    r.setNote((String) session.getAttributes().get("note"));

    return Mono.just(r);
}

We can get or set values using the .getAttributes() method, which returns a Map.

Let’s spin up our Spring app:

websession one

After the server is up and running, we can change the default WebSession values (0  and “Howdy Cosmic Spheroid!”). Run cURL command:

$ curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X GET http://localhost:8080/websession/test?id=222&note=helloworld

or visit the URL http://localhost:8080/websession/test?id=222&note=helloworld. Thereafter, the  JSON returned from localhost:8080/websession will display updated Session values:

websession three

That endpoint, localhost:8080/websession, returns the current WebSession attributes id and note.

6. Conclusion

We’ve learned how to add Spring WebSession to our WebFlux applications. For more information, check out the great official documentation.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW