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
FluxandMonoresponses - Better performance under load
๐ฆ 2. Required Dependencies
โ
Add to pom.xml:
org.springframework.boot
spring-boot-starter-data-mongodb-reactive
org.springframework.boot
spring-boot-starter-webflux
๐งฉ 3. Creating the MongoDB Model
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
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 {
}
You can define custom queries using @Query or derived method names.
๐ง 5. Reactive Service Layer (Optional but good practice)
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 findAll() {
return repository.findAll();
}
public Mono findById(String id) {
return repository.findById(id);
}
public Mono save(Product product) {
return repository.save(product);
}
public Mono deleteById(String id) {
return repository.deleteById(id);
}
}
๐ 6. Building the Reactive Controller
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 getAll() {
return service.findAll();
}
@GetMapping("/{id}")
public Mono getById(@PathVariable String id) {
return service.findById(id);
}
@PostMapping
public Mono create(@RequestBody Product product) {
return service.save(product);
}
@DeleteMapping("/{id}")
public Mono delete(@PathVariable String id) {
return service.deleteById(id);
}
}
๐งช 7. Running the App with a Test Dataset
๐งฐ Add application.properties
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:
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.