Backend Development 9 min read

Introduction to Spring WebFlux: Reactive Web Development with Spring

This article introduces Spring WebFlux, explains the concept of reactive programming, compares it with Spring WebMVC, describes its concurrency model, and provides complete code examples for building a reactive Spring Boot application using Mono, Flux, functional routing, and WebClient.

Top Architect
Top Architect
Top Architect
Introduction to Spring WebFlux: Reactive Web Development with Spring

Spring WebFlux is a reactive, non‑blocking web framework introduced in Spring 5 that can run on Netty, Undertow, or any Servlet container version 3.1 and above.

Reactive programming means that when an API call is made, the thread is not blocked; instead, the system processes other work and is notified when data becomes available, improving overall throughput.

The framework offers two core reactive types, Mono (for a single value) and Flux (for multiple values), and is built on the Reactor project.

Mono
person = personDao.getPerson(personId);
Flux
people = personDao.listAllPeople();

Spring WebFlux can be used alongside Spring WebMVC, or you can choose one of them depending on project requirements.

Choosing between Spring WebMVC and Spring WebFlux :

If an existing WebMVC project works well and most third‑party libraries are blocking, stay with WebMVC.

WebFlux offers flexibility in server choices (Netty, Tomcat, Jetty, Undertow) and programming style (Reactor, RxJava, functional routing).

For lightweight micro‑services or when you prefer Java 8 lambda style, WebFlux is recommended.

Mixing both frameworks in a micro‑service architecture is possible.

If the project heavily uses blocking APIs (e.g., JDBC), WebFlux may not be suitable.

The concurrency model differs: WebMVC uses a thread‑per‑request model with a limited thread pool, while WebFlux relies on a small number of event‑loop workers (e.g., Netty) and non‑blocking I/O, increasing request handling capacity.

Code example: adding the WebFlux starter

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

Define a simple domain object:

public class Person {
    private Integer id;
    private Integer age;
    private String name;
}

Configure functional routing:

@Configuration
public class PersonRouter {
    @Resource
    private PersonHandler personHandler;

    @Bean
    public RouterFunction
personRoutes() {
        return RouterFunctions.route()
            .GET("/person/{id}", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandler::getPerson)
            .GET("/person", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandler::listPeople)
            .POST("/person", personHandler::createPerson)
            .build();
    }
}

Handler implementation (service layer equivalent):

@Component
public class PersonHandler {
    @Resource
    private PersonRepository personDao;

    public Mono
listPeople(ServerRequest request) {
        Flux
people = personDao.listAllPeople();
        return ServerResponse.ok()
            .contentType(MediaType.APPLICATION_JSON)
            .body(people, Person.class);
    }

    public Mono
createPerson(ServerRequest request) {
        Mono
person = request.bodyToMono(Person.class);
        return ServerResponse.ok().build(personDao.savePerson(person));
    }

    public Mono
getPerson(ServerRequest request) {
        int personId = Integer.parseInt(request.pathVariable("id"));
        return personDao.getPerson(personId)
            .flatMap(p -> ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).bodyValue(p))
            .switchIfEmpty(ServerResponse.notFound().build());
    }
}

When the application starts, Spring WebFlux uses Netty by default. Accessing http://localhost:8080/person/1 will invoke the reactive endpoint and return JSON data.

Javabackend developmentReactive ProgrammingSpring BootSpring WebFlux
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.