Guide to Spring 5 WebFlux

1. Overview

Spring WebFlux is part of Spring 5 and provides reactive programming support for web applications.

In this tutorial, we’ll be creating a small reactive REST application using the reactive web components RestController and WebClient.

We will also be looking at how to secure our reactive endpoints using Spring Security.

2. 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 – routing and handling in another tutorial.

3. 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>

The latest spring-boot-starter-webflux can be downloaded from Maven Central.

4. Reactive REST Application

We’ll now build a very simple reactive REST EmployeeManagement application – using Spring WebFlux:

  • We’ll use a simple domain model – Employee with an id and a name field
  • We’ll build a REST API with a RestController to publish Employee resources as a single resource and as a collection
  • We’ll build a client with WebClient to retrieve the same resource
  • We’ll create a secured reactive endpoint using WebFlux and Spring Security

5. Reactive RestController

Spring WebFlux supports annotation-based configurations in the same way as the Spring Web MVC framework.

To begin with, on the server, we create an annotated controller that publishes a reactive stream of the Employee resource.

Let’s create our annotated EmployeeController:

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    private final EmployeeRepository employeeRepository;
    
    // constructor...
}

EmployeeRepository can be any data repository that supports non-blocking reactive streams.

5.1. Single Resource

Let’s create an endpoint in our controller that publishes a single Employee resource:

@GetMapping("/{id}")
private Mono<Employee> getEmployeeById(@PathVariable String id) {
    return employeeRepository.findEmployeeById(id);
}

We wrap a single Employee resource in a Mono because we return at most one employee.

5.2. Collection Resource

Let’s also add an endpoint that publishes the collection resource of all Employees:

@GetMapping
private Flux<Employee> getAllEmployees() {
    return employeeRepository.findAllEmployees();
}

For the collection resource, we use a Flux of type Employee – since that’s the publisher for 0..n elements.

6. Reactive Web Client

WebClient introduced in Spring 5 is a non-blocking client with support for reactive streams.

We can use WebClient to create a client to retrieve data from the endpoints provided by the EmployeeController.

Let’s create a simple EmployeeWebClient:

public class EmployeeWebClient {

    WebClient client = WebClient.create("http://localhost:8080");

    // ...
}

Here we have created a WebClient using its factory method create. It’ll point to localhost:8080 so we can use or relative URLs for calls made by this client instance.

6.1. Retrieving a Single Resource

To retrieve single resource of type Mono from endpoint /employee/{id}:

Mono<Employee> employeeMono = client.get()
  .uri("/employees/{id}", "1")
  .retrieve()
  .bodyToMono(Employee.class);

employeeMono.subscribe(System.out::println);

6.2. Retrieving a Collection Resource

Similarly, to retrieve a collection resource of type Flux from endpoint /employees:

Flux<Employee> employeeFlux = client.get()
  .uri("/employees")
  .retrieve()
  .bodyToFlux(Employee.class);
        
employeeFlux.subscribe(System.out::println);

We also have a detailed article on setting up and working with WebClient.

7. Spring WebFlux Security

We can use Spring Security to secure our reactive endpoints.

Let’s suppose we have a new endpoint in our EmployeeController. This endpoint updates Employee details and sends back the updated Employee.

Since this allows users to change existing employees, we want to restrict this endpoint to ADMIN role users only.

Let’s add a new method to our EmployeeController:

@PostMapping("/update")
private Mono<Employee> updateEmployee(@RequestBody Employee employee) {
    return employeeRepository.updateEmployee(employee);
}

Now, to restrict access to this method let’s create SecurityConfig and define some path-based rules to only allow ADMIN users:

@EnableWebFluxSecurity
public class EmployeeWebSecurityConfig {

    // ...

    @Bean
    public SecurityWebFilterChain springSecurityFilterChain(
      ServerHttpSecurity http) {
        http.csrf().disable()
          .authorizeExchange()
          .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
          .pathMatchers("/**").permitAll()
          .and()
          .httpBasic();
        return http.build();
    }
}

This configuration will restrict access to the endpoint /employees/update. Therefore only users having a role ADMIN will be able to access this endpoint and update an existing Employee.

Finally, the annotation @EnableWebFluxSecurity adds Spring Security WebFlux support with some default configurations.

We also have a detailed article on configuring and working with Spring WebFlux security.

8. Conclusion

In this article, we’ve explored how to create and work with reactive web components as supported by the Spring WebFlux framework. As an example, we’ve built a small Reactive REST application.

We learned how to use RestController and WebClient to publish and consume reactive streams.

We also looked into how to create a secured reactive endpoint with the help of Spring Security.

Other than Reactive RestController and WebClient, the WebFlux framework also supports reactive WebSocket and the corresponding WebSocketClient for socket style streaming of Reactive Streams.

We have a detailed article focused on working with Reactive WebSocket with Spring 5.

Finally, the complete source code used in this tutorial is available over on Github.

Related posts:

Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Implement ConcurrentSkipListMap API
Weak References in Java
Java Program to Implement LinkedList API
Java Program to Implement Suffix Tree
Java Program to Generate Random Hexadecimal Byte
Java Program to Implement Adjacency Matrix
Java Program to Implement Min Hash
Transactions with Spring and JPA
Java Program to Represent Linear Equations in Matrix Form
Java Program to Check the Connectivity of Graph Using BFS
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Perform Encoding of a Message Using Matrix Multiplication
A Guide to Java 9 Modularity
Từ khóa static và final trong java
Java Byte Array to InputStream
Send email with JavaMail
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Hash Tables with Quadratic Probing
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
A Quick Guide to Spring MVC Matrix Variables
Convert Time to Milliseconds in Java
Using Spring @ResponseStatus to Set HTTP Status Code
Hướng dẫn Java Design Pattern – Proxy
XML Serialization and Deserialization with Jackson
Spring @RequestMapping New Shortcut Annotations
Working with Network Interfaces in Java
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Implement Karatsuba Multiplication Algorithm
Java Program to Perform the Sorting Using Counting Sort
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Generate All Subsets of a Given Set in the Gray Code Order