Reactive MongoDB with Spring WebFlux : Integration

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:

  1. Why Reactive MongoDB?
  2. Required Dependencies
  3. Creating the MongoDB Model
  4. Reactive MongoDB Repository
  5. Reactive Service Layer (Optional)
  6. Building the Controller
  7. Running the App with a Test Dataset
  8. Final Thoughts
Reactive MongoDB Integration with Spring WebFlux

๐ŸŒ 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 and Mono responses
  • 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.

Reference

Spring WebFlux docs