What is Spring WebFlux?
Spring Webflux is a fully non-blocking, annotation-based web framework built on Project Reactor that allows you to build reactive applications on the HTTP layer. WebFlux uses a new router feature to apply functional programming to the web layer and bypass declarative controllers and Request Mappings.
It internally uses Project Reactor and its publisher implementations Flux and Mono. It supports two programming models:
- Annotation-based reactive components
- Functional routing and handling
Reactive programming represents a design paradigm that relies on asynchronous programming logic to handle real-time updates of static content. It uses automated data streams to provide an efficient means of handling data updates for your content whenever a user makes a request.
A data stream used in reactive programming is a coherently connected collection of digital signals that are generated continuously or nearly continuously. These data streams come from sources such as motion sensors, thermometers, and product inventory databases in response to triggers.
Let’s start with the spring-boot-starter-webflux dependency
- spring-boot and spring-boot-starter for basic Spring Boot application setup
- spring-webflux framework
- reactor-core that we need for reactive streams and also reactor-netty
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> <version>2.6.4</version> </dependency>
Spring Boot WebFlux example
In the following application, I’m creating a simple Spring Boot web application with reactive support.
Required dependencies for the application:
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.
application.properties, we turn off the Spring Boot banner by doing
Now, we have a simple rest controller, a simple REST endpoint, which returns a message. The home() method return type is a Publisher. Mono.just() emits the specified string message.
and we have a SpringWebFluxApplication class,
This code sets up the Spring Boot application. We run the application and navigate to
In this blog, we explored reactive web components as supported by the Spring WebFlux framework and we also covered the dependency which we have to use in spring WebFlux we also created a small application to get better info about Spring Web Flux.