Guide to Spring WebFlux

Originally published at

1. Overview

Spring WebFlux framework 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

3. Dependencies

Let's start with the spring-boot-starter-webflux dependency, which actually 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

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 REST APIs for publishing and retrieve Single as well as Collection Employee resources using RestController and WebClient
  • And we will also be creating a secured reactive endpoint using WebFlux and Spring Security

5. Reactive RestController

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

To begin with, on the server side, we create an annotated controller that publishes our reactive streams of Employee.

Let's create our annotated EmployeeController:

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:

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

For a single Employee resource, we have used a Mono of type Employee because it will emit at most 1 element.

5.2. Collection Resource

Let's also add an endpoint in our controller that publishes the collection resource of all Employees:

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

For the collection resource, we have used Flux of type Employee – since that's the publisher focused on emitting 0..n elements.

6. Reactive Web Client

WebClient introduced in Spring 5 is a non-blocking client with support for Reactive Streams.

On the client side, we use WebClient to retrieve data from our endpoints created in 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 for relative URLs.

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")

6.2. Retrieving Collection Resource

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

Flux<Employee> employeeFlux = client.get()

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:

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:

public class EmployeeWebSecurityConfig {
    // ...
    public SecurityWebFilterChain springSecurityFilterChain(
      ServerHttpSecurity http) {
          .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")

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.

8. Conclusion

In this article, we've explored how to create and work with reactive web components supported by Spring WebFlux framework by creating a small Reactive REST application.

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

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

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

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

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

Spring & Hibernate for Beginners (includes Spring Boot)

Spring Framework Master Class - Learn Spring the Modern Way!

Master Microservices with Spring Boot and Spring Cloud

Spring Boot and OAuth2: Getting the Authorization Code

An Introduction to Spring Boot

How to build GraphQL APIs with Kotlin, Spring Boot, and MongoDB?

Build a Rest API with Spring Boot using MySQL and JPA

Angular 8 + Spring Boot 2.2: Build a CRUD App Today!

Spring Boot vs. Spring MVC vs. Spring: How Do They Compare?

Top 4 Spring Annotations for Java Developer in 2019

#spring-boot #java

What is GEEK

Buddha Community

Guide to Spring WebFlux

Spring vs Spring BooDifference Between Spring and Spring Boot

As an extension of the Spring Framework, Spring Boot is widely used to make development on Spring faster, more efficient and convenient. In this article, we will look at some of the parameters were using Spring Boot can drastically reduce the time and effort required in application development.

What is Spring?

Spring Boot

Difference between Spring and Spring Boot

Advantages of Spring Boot over Spring


#full stack development #spring #spring and spring boot #spring boot

Spring Native turns Spring apps into native executables

Spring Native beta release leverages GraalVM to compile Spring Java and Kotlin applications to native images, reducing startup time and memory overhead compared to the JVM.

Spring Native, for compiling Spring Java applications to standalone executables called native images, is now available as a beta release. Native images promise faster startup times and lower runtime memory overhead compared to the JVM.

Launched March 11 and available on, the Spring Native beta compiles Spring applications to native images using the GraalVM multi-language runtime. These standalone executables offer benefits including nearly instant startup (typically fewer than 100ms), instant peak performance, and lower memory consumption, at the cost of longer build times and fewer runtime optimizations than the JVM.

#spring native turns spring apps into native executables #spring native #spring #native executables #spring apps

Spring Framework Tutorial

What is the spring framework in Java?

The spring framework is one of the most versatile frameworks in java which is used to bring down the complexity of the development of enterprise-grade applications. The first production release of the spring framework was in March 2004 and since then, this robust and open-source framework has gained tremendous popularity, so much so that it is often referred to by developers all around the world as the “framework of frameworks”. Spring is a loosely coupled, open-source application framework of java. It is lightweight and the inversion of the control container for the Java platform. A large number of Java applications use the core features of the spring framework. In addition to that, extensions have also been developed to allow developers to develop Web Applications on top of the Java Enterprise Edition platform.

#spring #spring-framework #java #spring framework tutorial #why should one learn about the spring framework? #what is the spring framework in java?

Comprehensive Guide to Understand Spring Bean Scopes

Written by Rod Johnson, Spring framework was first released under the Apache 2.0 license in June 2003. It is the most common application development platform for enterprise Java. This open-source Java platform is used by millions of users across the globe to build high-performing, conveniently verifiable, and reusable code.

What is Spring Bean?

An object that is instantiated, gathered, and otherwise managed by a Spring IoC container is called a bean. The configuration metadata that you provide to the container is used to build these beans. The configuration metadata that you provide to the container is used to build these beans. The configuration metadata contained in the bean specification is required for the container to know how to make a bean, details of bean’s lifespan and timeline and its dependencies.

#spring #spring-framework #learn-spring-framework #java #backend #model-view-controller #spring-bean-scopes #hackernoon-top-story

Spring integration: Provides an Extension Of The Spring Programming

Spring Integration

Code of Conduct

Please see our Code of conduct.

Reporting Security Vulnerabilities

Please see our Security policy.

Checking out and Building

To check out the project and build from the source, do the following:

git clone git://
cd spring-integration
./gradlew clean test


./gradlew clean testAll

The latter runs additional tests (those annotated with @LongRunningIntegrationTest); it is a more thorough test but takes quite a lot longer to run.

The test results are captured in build/reports/tests/test (or .../testAll) under each module (in HTML format).

Add --continue to the command to perform a complete build, even if there are failing tests in some modules; otherwise the build will stop after the current module(s) being built are completed.

NOTE: While Spring Integration runs with Java SE 8 or higher, a Java 11 compiler is required to build the project.

To build and install jars into your local Maven cache:

./gradlew publishToMavenLocal

To build api Javadoc (results will be in build/api):

./gradlew api

To build the reference documentation (results will be in build/docs/asciidoc and build/docs/asciidocPdf):

./gradlew reference

To build complete distribution including -dist, -docs, and -schema zip files (results will be in build/distributions):

./gradlew dist

Using Eclipse or Spring Tool Suite (with BuildShip Plugin)

If you have the BuildShip plugin installed,

File -> Import -> Gradle -> Existing Gradle Project

Using Eclipse or Spring Tool Suite (when the BuildShip Plugin is not installed)

To generate Eclipse metadata (.classpath and .project files, etc), do the following:

./gradlew eclipse

Once complete, you may then import the projects into Eclipse as usual:

File -> Import -> General -> Existing projects into workspace

Browse to the 'spring-integration' root directory. All projects should import free of errors.

Using IntelliJ IDEA

To import the project into IntelliJ IDEA:

File -> Open... -> and select build.gradle from spring-integration project root directory


See also Contributor Guidelines.


For more information, please visit the Spring Integration website at:

Download Details:
Author: spring-projects
Source Code:
License: Apache-2.0 License

#spring #spring-framework #spring-boot #java