Spring WebFlux Tutorial

In this tutorial, we will learn about Spring WebFlux and how to build reactive REST APIs using Spring WebFlux, Spring Boot, and MongoDB database.

Reactive programming is a programming paradigm where the focus is on developing asynchronous and non-blocking applications in an event-driven form,

Reactive Streams Specification

Reactive Streams Specification is a set of rules or set of guidelines that you need to follow when designing a reactive stream. 

These specifications introduce four interfaces that should be used and overridden when creating a reactive stream.


The Publisher is a data source that will always publish events. 


The Subscriber will subscribe/consume the events from the Publisher.


The Subscription represents the unique relationship between a Subscriber and a Publisher interface.


It represents a processing stage – which is both a Subscriber and a Publisher and MUST obey the contracts of both.

Reactive Programming Libraries

A reactive library is nothing but the implementation of reactive specification interfaces. Here are some reactive libraries that are available to us:
  • Project Reactor 
  • RxJava 
  • JDK 9 Flow Reactive Stream
In this tutorial, we are going to use Project Reactor reactive library.

Project Reactor

The Project Reactor is a fourth-generation reactive library, based on the Reactive Streams specification, for building non-blocking applications on the JVM.

Mono and Flux Implementations

Project reactor libraries provide two implementations of the Publisher interface:
  1. Mono
  2. Flux
Mono: Returns 0 or 1 element.
The Mono API allows producing only one value.

Flux: Returns 0…N elements.
The Flux can be endless, it can produce multiple values.

Mono vs Flux

Mono and Flux are both implementations of the Publisher interface. In simple terms, we can say that when we're doing something like a computation or making a request to a database or an external service, and expecting a maximum of one result, then we should use Mono.

When we're expecting multiple results from our computation, database, or external service call, then we should use Flux.

Spring WebFlux Overview

  • Spring WebFlux is a reactive web framework introduced in Spring Framework 5.
  • It is designed to provide a reactive programming model for building web applications and APIs using Spring. 
  • Spring WebFlux is built on top of Project Reactor, which is a popular reactive programming library for the JVM. 
  • Spring WebFlux has supported Tomcat, Jetty, and Servlet 3.1+ containers, as well as on non-Servlet runtimes such as Netty and Undertow.

Reactive Database Drivers

To connect the Spring Webflux application with the database, we have to use a database driver that supports reactive stream.
Some of the supported reactive drivers in Spring WebFlux include:
  • Reactive MongoDB driver: This driver provides reactive support for MongoDB. 
  • Reactive Redis driver: This driver provides reactive support for Redis, an in-memory key-value store. 
  • Reactive Cassandra driver: This driver provides reactive support for Cassandra.
In this tutorial, we are going to use Reactive MongoDB Driver to connect to the MongoDB database.

Spring Boot + Spring WebFlux Application Architecture

We are going to use three-layer architecture:

Controller Layer

In a controller layer, we will build reactive REST APIs that return responses as Publisher type (Mono or Flux)

Service Layer

In a service layer, we will create service interfaces and classes and we will keep all the business logic.
All the methods in a service layer should return as Publisher type (Mono or Flux).

Repository Layer

In a service layer, we will keep all the database-related logic. All the methods in a repository layer should return as  Publisher type (Mono or Flux).

Programming Models Supported by Spring WebFlux

Spring WebFlux supports two types of programming models : 
  1. A traditional annotation-based model with @Controller, @RequestMapping, and other annotations that you have been using in Spring MVC. 
  2. A brand new Functional style model based on Java 8 lambdas for routing and handling requests.
In this tutorial, we are going to use the annotation-based model to build the reactive REST APIs using Spring WebFlux.

What we will build?

We’ll build Reactive REST APIs for creating, retrieving, updating, and deleting an Employee

All the REST APIs will be asynchronous and will return a Publisher (Mono or Flux).

Here is the complete tutorial: 

All Spring WebFlux Tutorials

Spring Boot WebFlux MongoDB CRUD REST API Tutorial

In this tutorial, you will learn how to build CRUD REST APIs using Spring Boot, Spring WebFlux, and MongoDB NoSQL database.

Testing Spring WebFlux Reactive CRUD Rest APIs using WebTestClient

In this tutorial, we will learn how to write Integration tests to test Spring WebFlux reactive CRUD REST APIs using WebTestClient.

Spring WebFlux Functional Endpoints CRUD REST API Example

In this tutorial, we will new functional-style programming model to build reactive CRUD REST APIs using Spring Boot 3, Spring WebFlux, MongoDB, and IntelliJ IDEA.

Unit Testing Spring WebFlux CRUD REST API using JUnit and Mockito

In this tutorial, we will learn how to unit test Spring WebFlux controller (Reactive CRUD REST APIs) using JUnit and Mockito frameworks.