Close

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

Example

Maven Dependencies

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicbig.example</groupId>
<artifactId>spring-reactive-helloword</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.8.RELEASE</version>
</plugin>
</plugins>
</build>

</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

@Controller
public class MyController {

  @GetMapping("/")
  @ResponseBody
  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

@SpringBootApplication
@EnableWebFlux
public class ExampleApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(ExampleApplication.class);
    }
}

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

Output

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
                • MyController.java

    See Also