Spring WebFlux Reactive CRUD REST API Example
Last Updated :
28 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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read