1. Overview

In this article, we will see spring web flux framework examples and will cover a little about spring web flux framework use.

2. Spring web flux framework

In the traditional programming model, all services are synchronous and blocking in nature. Reactive programming came into the picture to provide an asynchronous and non-blocking programming model.

Reactive streams are used to achieve asynchronous and non-blocking backpressure. Reactive streams are the standard and specification for Stream-oriented libraries for the JVM. Rxjava, project reactor and more are implementations of the reactive streams API specification.

Spring web flux is a mirror version of spring MVC and it supports fully non-blocking reactive streams. Spring web flux internally uses a project reactor which is one of the implementations of the reactive library.

Spring web flux framework is available from spring 5 and provides reactive programming support for web applications.

Next, we are going to implement non-blocking services by using spring web flux, spring boot, and mango database.

3.Spring web flux example

3.1 Get started

we should add spring-boot-starter-webflux dependency to enable reactive programming in the application and add spring-boot-starter-data-mongodb-reactive dependency to get non-blocking and event-driven features from mongo database side.

To get reactive programming features, everything should be non-blocking in the request like from request making to getting data from the database.

At the time of writing this article, Spring did not provide fully featured connectivity API for reactive relational database integration. If you want more information why it is implemented yet while relational databases are most popular. Go through the following link


In pom.xml



3.2 Project Structure

Spring Web Flex Project Structure

3.3 Mono and Flux

Spring web flux internally uses the project reactor library. Mono and Flux are Publisher interface implementations.

Mono represents 0…1 elements
Flux represents 0…N elements

Spring web flux framework provides two types of programming models

1.Annotated controllers
2.Functional Endpoints

In this article, we implemented REST API with annotated controllers and have look at the following controller class

public class MarginController {
    private MarginRepository marginRepository;
    // create margin
    public Mono<MarginDo> createMargin(@RequestBody MarginDo marginDo) {
        return marginRepository.save(marginDo);
    // update margin
    public void updateMargin(@RequestBody MarginDo marginDo) {
    // get margin by customer id
    public Mono<MarginDo> getCustomerMargin(@PathVariable(value = "customerId") String customerId) {
        return marginRepository.findById(customerId);
    // get all margins
    public Flux<MarginDo> getAllMargins() {
        return marginRepository.findAll();
    // delete margin by customer id
    public void deleteMargin(@PathVariable(value = "customerId") String customerId) {
    // stream live margins
    @GetMapping(value = "/stream/margins", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<MarginDo> streamAllMargins() {
        return marginRepository.findAll();

Application.properties file


Note: while testing method streamAllMargins(), use browser then we can see event-stream output printing in the browser one by one like as in the following image

Spring Web Flex Output

3.4 Mango reactive repository

Use the ReactiveMongoRepository interface to get all basic Dao layer methods that are non-blocking in nature.

public interface MarginRepository extends
 ReactiveMongoRepository<MarginDo, String> { 

4. Conclusion

We have explained the spring web flux framework importance with example clearly.

Hope 😊 you like it. if you want more information you can through reference links provided in section 6.

5. Spring Web Flux Framework Source Code

Complete example code is available in the following git repository.

Git Repository link: https://github.com/subhashlamba/spring-examples






Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *