Spring 5 WebFlux - Reactive Web Application Getting Started Example

[Last Updated: Jan 18, 2020]

Spring 5 introduced WebFlux Framework which supports Reactive Streams API to run non-blocking web applications. This is the same concept which is also adopted in Java 9 (example here).

In a WebFlux non-blocking web application, the application components act as the Publishers which produce data. The HTTP server act as a Subscriber which can control (by using non-blocking back pressure feature) how fast or slow a Publisher can produce data. The benefit of this ability is to scale with a fixed number of threads and limited memory under load situations.

Spring WebFlux uses Reactor library as an implementation of non-blocking reactive streams. This library is developed in close collaboration with Spring. The main classes which we will be using are Mono and Flux. These classes are implementations of Producer and support data sequences of 0..1 and 0..N respectively.

Spring provides two programming models in WebFlux: Annotation based Controller (which is consistent to Spring MVC) and Functional Endpoints.

Following Hello world example will use Annotation based controller with Spring Boot 2.0.0.RELEASE


Maven Dependencies


<project .....>



spring-boot-starter-webflux is a starter for building WebFlux application. Spring Boot 2 uses Netty Server for reactive streams by default.

The Controller

public class MyController {

  public Publisher<String> handler() {
      return Mono.just("Hello world!");

Mono produces a zero or single value, whereas Flux can produce multiple values. Spring WebFlux writes individual values from producers as they become available as opposed to the async processing (example here) where only full response can be written when the thread created by the async process exits.

JavaConfig and main class

public class ExampleApplication {

    public static void main(String[] args) throws Exception {;

The annotation @EnableWebFlux imports the Spring Web Reactive configuration from WebFluxConfigurationSupport class.

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run


Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.0.0.RELEASE
    Corresponding Spring Version 5.0.4.RELEASE
  • spring-boot-starter-webflux : Starter for building WebFlux applications using Spring Framework's Reactive Web support.
  • JDK 1.8
  • Maven 3.3.9

Spring WebFlux Reactive Web Example Select All Download
  • spring-reactive-helloworld
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also