Spring WebFlux Reactive CRUD REST API Example
Last Updated :
24 Apr, 2025
Spring WebFlux can be defined as the reactive programming framework provided by the Spring ecosystem for the building of asynchronous, non-blocking, and event-driven applications and it can be designed to handle a large number of concurrent connections while consuming less resources.
Key Terminologies:
- Mono and Flux: Mono can represent the publisher that can emit at most one item, and it can emit either a single value or an error. Flux can represent the publisher that emits zero or more items, and it can emit the streams of the data asynchronously.
- Router Function: A router function can be defined as the mapping between request paths and handler functions, and it can allow us to define routes programmatically, mapping incoming requests to appropriate the handler functions.
Steps to Implement Spring WebFlux Reactive CRUD REST API
We can develop the simple Spring WebFlux reactive application that can save the user data after that we can retrieve, update, and delete the data of the user into the Spring WebFlux application.
Step 1: Create the spring project using spring initializer on creating the project add the below dependencies into the project.
Dependencies:
- Spring Reactive Web
- Spring Data Reactive MongoDB
- Spring Dev Tools
- Lombok
Once Create the spring project then the file structure looks like the below image.

Step 2: open the application.properties file and put the below code for the server port and mongodb database configuration to the project.
server.port= 8085
spring.data.mongodb.uri= mongodb://localhost:27017/springreactive
Step 3: Create the new package and it named as the model in that package create the new Java class and it named as User.
Go to src > com.gfg.springreactivecrudapp > model > User and put the below code.
Java
package com.gfg.springreactivecrudapp.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* Represents a User entity.
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class User {
@Id
private String id;
private String name;
private String qualification;
private String gender;
private int age;
}
Step 4: Create a new package and named it as the repository. In that package, create the new Java interface and named it as UserRepository.
Go to src > com.gfg.springreactivecrudapp > repository > UserRepository and put the below code.
Java
package com.gfg.springreactivecrudapp.repository;
import com.gfg.springreactivecrudapp.model.User;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
/**
* Repository interface for User entities.
*/
@Repository
public interface UserRepository extends ReactiveCrudRepository<User, String> {
}
Step 5: Create the new package and it named as the configuration in that package create the new Java class and it named as UserHandler.
Go to src > com.gfg.springreactivecrudapp > configuration > UserHandler and put the below code.
Java
package com.gfg.springreactivecrudapp.configuration;
import com.gfg.springreactivecrudapp.model.User;
import com.gfg.springreactivecrudapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Handler class to handle HTTP requests related to User entities.
*/
@Component
public class UserHandler {
@Autowired
private UserRepository userRepository;
/**
* Retrieve all users.
*/
public Mono<ServerResponse> getAllUsers(ServerRequest request) {
Flux<User> users = userRepository.findAll();
return ServerResponse.ok().body(users, User.class);
}
/**
* Retrieve a user by ID.
*/
public Mono<ServerResponse> getUserById(ServerRequest request) {
String userId = request.pathVariable("id");
Mono<User> userMono = userRepository.findById(userId);
return userMono.flatMap(user -> ServerResponse.ok().bodyValue(user))
.switchIfEmpty(ServerResponse.notFound().build());
}
/**
* Create a new user.
*/
public Mono<ServerResponse> createUser(ServerRequest request) {
Mono<User> userMono = request.bodyToMono(User.class);
return userMono.flatMap(user -> userRepository.save(user))
.flatMap(savedUser -> ServerResponse.ok().bodyValue(savedUser));
}
/**
* Update an existing user.
*/
public Mono<ServerResponse> updateUser(ServerRequest request) {
String userId = request.pathVariable("id");
Mono<User> userMono = request.bodyToMono(User.class);
return userMono.flatMap(user -> {
user.setId(userId);
return userRepository.save(user);
}).flatMap(savedUser -> ServerResponse.ok().bodyValue(savedUser))
.switchIfEmpty(ServerResponse.notFound().build());
}
/**
* Delete a user by ID.
*/
public Mono<ServerResponse> deleteUser(ServerRequest request) {
String userId = request.pathVariable("id");
return userRepository.deleteById(userId)
.then(ServerResponse.ok().build())
.switchIfEmpty(ServerResponse.notFound().build());
}
}
Step 6: Create the new package and it named as the configuration in that package create the new Java class and it named as UserRouter.
Go to src > com.gfg.springreactivecrudapp > configuration > UserRouter and put the below code.
Java
package com.gfg.springreactivecrudapp.configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
/**
* Configures the routes for handling HTTP requests related to users.
*/
@Configuration
public class UserRouter {
@Autowired
private UserHandler userHandler;
/**
* Defines the routes for handling user-related HTTP requests.
*/
@Bean
public RouterFunction<ServerResponse> userRoutes() {
return RouterFunctions
.route(GET("/users"), userHandler::getAllUsers)
.andRoute(GET("/users/{id}"), userHandler::getUserById)
.andRoute(POST("/users"), userHandler::createUser)
.andRoute(PUT("/users/{id}"), userHandler::updateUser)
.andRoute(DELETE("/users/{id}"), userHandler::deleteUser);
}
}
Step 7: Open the main class and add the @EnableReactiveMongoRepositories into it.
Java
package com.gfg.springreactivecrudapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
/**
* Main class to start the Spring Reactive CRUD application.
*/
@SpringBootApplication
@EnableReactiveMongoRepositories
public class SpringReactiveCrudAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringReactiveCrudAppApplication.class, args);
}
}
Step 8: Once complete the spring project and it run as spring application once it runs successful then it starts at port 8085.

Endpoints Outputs:
Create the User:
POST http://localhost:8085/users

Update the User:
PUT http://localhost:8085/users/{userid}

Delete the User:
DELETE http://localhost:8085/users/{userid}

Get all Users:
GET http://localhost:8085/users

Get UserById:
GET http://localhost:8085/users/{userid}

We the follow the above steps and can successfully build the spring WebFlux reactive crud application and test the endpoints using postman.
Similar Reads
Spring WebFlux Functional Endpoints CRUD REST API Example
Spring Boot is a Java framework for back-end development. Another miracle in Spring Boot is the Spring Boot Community has developed the Spring Reactive Web Framework i.e. Spring WebFlux. To develop this project, we have used the MongoDB database. Here, for every CRUD (Create, Retrieve, Update, Delet
7 min read
Testing Spring WebFlux Reactive CRUD Rest APIs Using WebTestClient
Spring Boot is one of the famous frameworks for Back-end development. The Spring Boot Community Developed the Spring Reactive Web Framework. This Reactive Framework is available from Spring Boot 5.0 and Above versions only. The Spring Reactive allows developers to build Asynchronous, Non-Blocking, a
8 min read
Spring WebFlux Rest API Global Exception Handling
Spring WebFlux is part of Spring Framework, allowing us to Reactive programming and Support non-blocking I/O operations. The Spring Framework provides a lot of Annotations to handle the applications. This article focuses on Global Exception Handling by using Rest API in the Spring WebFlux. For this,
6 min read
Spring MVC and Hibernate CRUD Example
In this article, we will be developing CRUD operations in Spring MVC and Hibernate. Hibernate is an object-relational mapping (ORM) framework. Developers use Hibernate to interact with databases using Java objects rather than SQL queries. Spring MVC is a Model-View-Controller (MVC) framework used to
6 min read
How to Redirect a Request in Spring WebFlux?
The Spring WebFlux is a part Spring Reactive framework. In Spring WebFlux, we can redirect requests by using the ServerResponse. This is one of the classes in Spring WebFlux which provides methods for creating responses. To direct a request in Spring WebFlux, we typically return the ServerResponse w
4 min read
Creating REST APIs Using Spring WebFlux and MongoDB
Spring Boot is the most popular Java framework for building stand-alone Java-based applications quickly and with minimal configuration. WebFlux is a responsive operating system provided by the Spring framework for running non-blocking, asynchronous, and event-driven applications. On the other hand,
10 min read
Containerize Spring WebFlux Application
In todayâs Web development, containerization has become a dominant way of deploying and managing applications. The containers package application provides a consistent environment in the development and production phases. Docker is a popular tool for building, deploying, and running applications usi
3 min read
Spring MVC CRUD with Example
In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows
7 min read
Building Reactive Microservices with Spring WebFlux
This article will teach us how to build reactive microservices with Spring WebFlux using a related example. Here, we create a student microservice to handle students and their data. For understanding purposes, we provide basic features and functionalities such as adding a student, searching for a st
5 min read
Retry in Spring WebFlux
The Spring WebFlux is part of the Spring Framework And It allows developers to develop nonblocking applications. In reactive programming lot of operators are available to handle to publishers and consumers. In this article, we will explain about retry operator in WebFlux with related examples. Retry
5 min read