Guide to Spring 5 WebFlux
# Guide to Spring 5 WebFlux
# Overview
origin document link (opens new window)
Spring 5 includes Spring WebFlux, which provides reactive programming support for web applications. In this tutorial, we'll create a small reactive REST application using the reactive web components RestController and WebClient. We'll also look at how to secure our reactive endpoints using Spring Security.
# Spring WebFlux Framework
Spring WebFlux internally uses Project Reactor and its publisher implementations, Flux and Mono.
The new framework supports two programming models:
- Annotation-based reactive components
- Functional routing and handling
We'll focus on the annotation-based reactive components, as we already explored the functional style (opens new window) – routing and handling in another tutorial.
# Dependencies
Let's start with the spring-boot-starter-webflux dependency, which pulls in all other required dependencies:
- 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.2.6.RELEASE</version>
</dependency>
2
3
4
5
The latest spring-boot-starter-webflux (opens new window) can be downloaded from Maven Central.
# Reactive REST Application
Now we'll build a very simple reactive REST EmployeeManagement application using Spring WebFlux:
- Use a simple domain model – Employee with an id and a name field
- Build a REST API with a RestController to publish Employee resources as a single resource and as a collection
- Build a client with WebClient to retrieve the same resource
- Create a secured reactive endpoint using WebFlux and Spring Security