Ebooks

Spring Boot WebFlux tutorial

Spring Boot WebFlux tutorial shows how to create a simple Spring Boot reactive web application with WebFlux.

WebFlux

WebFlux is a Spring reactive-stack web framework. It was added to Spring 5. It is fully non-blocking, supports reactive streams back pressure, and runs on such servers such as Netty, Undertow, and Servlet 3.1+ containers.

Spring WebFlux is an alternative to the traditional Spring MVC.

Spring WebFlux internally uses Project Reactor and its publisher implementations Flux and Mono. It supports two programming models: a) annotation-based reactive components, b) functional routing and handling.

Reactive programming

Reactive programming is a programming paradigm that is functional, event-based, non-blocking, asynchronous, and centered around data stream processing. The term reactive comes from the fact that we react to changes such as mouse clicks or I/O events.

Reactive applications scale better and are more efficient when we are dealing with lots of streaming data. Reactive applications are non-blocking; they're not using resources waiting for processes to finish.

Reactive applications implement an event-based model where data is pushed to the consumer. The consumer of data, a subscriber, subscribes to the publisher, which publishes asynchronous streams of data.

Spring Reactor

Spring Reactor is a reactive library for building non-blocking applications on the JVM based on the Reactive Streams Specification.

The Reactor Project offers two types of publishers: Mono and Flux. Flux is a publisher that produces 0 to N values. Operations that return multiple elements use this type. Mono is a publisher that produces 0 to 1 value. It is used for operations that return a single element.

Spring Boot WebFlux example

In the following application we create a simple Spring Boot web application with reactive support.

pom.xml
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│           application.properties
└───test
    └───java

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>springbootwebfluxsimple</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

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

        </plugins>
    </build>
</project>

This is the Maven pom.xml file. The spring-boot-starter-webflux is a Spring Boot starter for building WebFlux applications using Spring Framework's Reactive Web support.

resources/application.properties
spring.main.banner-mode=off

In the application.properties, we turn off the Spring Boot banner.

com/zetcode/MyController.java
package com.zetcode.controller;

import org.reactivestreams.Publisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class MyController {

    @GetMapping("/")
    public Publisher<String> home() {

        return Mono.just("Home page");
    }
}

We have a simple REST endpoint, which returns a message. The home() method return type is a Publisher. Mono.just() emits the specified string message.

com/zetcode/Application.java
package com.zetcode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application  {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

This code sets up the Spring Boot application.

$ mvn spring-boot:run

We run the application and navigate to localhost:8080.

In this tutorial, we have created a simple Spring Boot WebFlux application.

List all Spring Boot tutorials.