In this post learn how to integrate Reactive MongoDB with Spring WebFlux by utilizing reactive repositories along with Flux and Mono for non-blocking data access.
๐ Table of Contents:
- Why Reactive MongoDB?
- Required Dependencies
- Creating the MongoDB Model
- Reactive MongoDB Repository
- Reactive Service Layer (Optional)
- Building the Controller
- Running the App with a Test Dataset
- Final Thoughts

๐ 1. Why Reactive MongoDB?
MongoDB’s native asynchronous driver integrates seamlessly with Spring Data MongoDB Reactive, allowing full non-blocking access. When combined with WebFlux, it results in a highly scalable and event-driven stack ideal for modern applications.
Benefits:
- Full non-blocking MongoDB access
- Supports
Flux
andMono
responses - Better performance under load
๐ฆ 2. Required Dependencies
โ
Add to pom.xml
:
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> |
๐งฉ 3. Creating the MongoDB Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.kscodes.springboot.reactive.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document("products") public class Product { @Id private String id; private String name; private double price; // Getters and Setters } |
๐๏ธ 4. Reactive MongoDB Repository
1 2 3 4 5 6 7 8 9 10 |
package com.kscodes.springboot.reactive.repository; import com.kscodes.springboot.reactive.model.Product; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; public interface ProductRepository extends ReactiveMongoRepository<Product, String> { } |
You can define custom queries using @Query
or derived method names.
๐ง 5. Reactive Service Layer (Optional but good practice)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.kscodes.springboot.reactive.service; import com.kscodes.springboot.reactive.model.Product; import com.kscodes.springboot.reactive.repository.ProductRepository; import org.springframework.stereotype.Service; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @Service public class ProductService { private final ProductRepository repository; public ProductService(ProductRepository repository) { this.repository = repository; } public Flux<Product> findAll() { return repository.findAll(); } public Mono<Product> findById(String id) { return repository.findById(id); } public Mono<Product> save(Product product) { return repository.save(product); } public Mono<Void> deleteById(String id) { return repository.deleteById(id); } } |
๐ 6. Building the Reactive Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.kscodes.springboot.reactive.controller; import com.kscodes.springboot.reactive.model.Product; import com.kscodes.springboot.reactive.service.ProductService; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @RestController @RequestMapping("/products") public class ProductController { private final ProductService service; public ProductController(ProductService service) { this.service = service; } @GetMapping public Flux<Product> getAll() { return service.findAll(); } @GetMapping("/{id}") public Mono<Product> getById(@PathVariable String id) { return service.findById(id); } @PostMapping public Mono<Product> create(@RequestBody Product product) { return service.save(product); } @DeleteMapping("/{id}") public Mono<Void> delete(@PathVariable String id) { return service.deleteById(id); } } |
๐งช 7. Running the App with a Test Dataset
๐งฐ Add application.properties
1 2 3 4 5 6 |
spring.data.mongodb.database=reactive_db spring.data.mongodb.port=27017 spring.data.mongodb.host=localhost |
Ensure MongoDB is running locally or use a Docker image:
1 2 3 4 |
docker run -d -p 27017:27017 --name reactive-mongo mongo |
โ 8. Final Thoughts
Spring WebFlux and Reactive MongoDB form a powerful combination for high-performance and scalable applications. You get the benefit of a unified non-blocking stack across both data access and HTTP layers.