Spring Boot WebFlux tutorial

The Spring Boot WebFlux tutorial shows how to create a simple Spring Boot responsive Web application using WebFlux.

WebFlux

WebFlux is a Spring responsive Web framework. It has been added to Spring 5. It is completely non-blocking, supports Reactive Streams response streams, and works well on servers such as Netty, Undertow, and Servlet 3.1 + containers.

Spring WebFlux is an alternative to traditional Spring MVC.

Spring WebFlux internally implements Flux and Mono using Project Reactor and its Publisher. It supports two programming models: a) annotation-based responsive components, and b) function-level routing and processing.

Responsive programming

Reactive programming is a programming paradigm that is functional, event-based, non-blocking, asynchronous, and data-flow-centric. The term “responsive” comes from the fact that we react to changes such as mouse clicks or I/O events.

Responsive applications scale better and are more efficient when we are dealing with large amounts of streaming data. Reactive applications are non-blocking; They are not using resources to wait for the process to complete.

Reactive applications push data to consumers based on an event-based model. Data consumers, subscribers, and subscription publishers publish asynchronous data streams.

Spring Reactor

Spring Reactor is a responsive library for building non-blocking applications on the JVM based on responsive flow specifications.

The Reactor project provides two types of publishers: Mono and Flux. Flux is a publisher that produces 0 to N values. Operations that return more than one element use this type. Mono is the publisher that produces 0 to 1 values. 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 responsiveness.

Pom. XML SRC ├ ─ ─ ─ the main │ ├ ─ ─ ─ Java │ │ └ ─ ─ ─ com │ │ └ ─ ─ ─ zetcode │ │ │ Application. Java │ │ └ ─ ─ ─ the controller │ │ ├ ─ 07.02.55 myController. Java ├ ─ 07.02.55Copy the code

This is the project structure.

pom.xml

<? The 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" > The < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Zetcode < / groupId > < artifactId > springbootwebfluxsimple < / artifactId > <version> 1.0-snapshot </version> <packaging>jar</packaging> <properties> <java.version>13</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.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>Copy the code

This is Maven’s POM.xml file. Spring-boot-starter-webflux is an initiator of Spring Boot that uses the Reactive Web of the Spring Framework to build WebFlux applications.

resources/application.properties

spring.main.banner-mode=off

Copy the code

In the application.properties file, we turn off Spring Boot’s banner mode.

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"); }}Copy the code

We have a simple REST endpoint that returns a message. The home() method returns type 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); }}Copy the code

This code runs the Spring Boot application.

$ mvn spring-boot:run

Copy the code

We run the application and type localhost:8080 in the browser url bar.

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