๐ Table of Contents:
- What is Spring WebFlux?
- Setting Up the Project
- Creating Reactive Domain & Repository
- Building the Reactive REST Controller
- Handling Errors in Reactive APIs
- Testing the Reactive Endpoints
- Final Thoughts on Reactive REST APIs with Spring WebFlux

๐ 1. What is Spring WebFlux?
Spring WebFlux is a reactive web framework built on Project Reactor, supporting fully non-blocking, asynchronous communication. It enables efficient handling of concurrent requests by leveraging reactive streams.
Unlike traditional Spring MVC, WebFlux is designed for:
- Streaming data
- Event-driven architecture
- High concurrency systems
โ๏ธ 2. Setting Up the Project
๐งฑ Maven Dependencies:
1 2 3 4 5 6 7 8 9 10 11 12 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> </dependency> |
๐งฉ 3. Creating Reactive Domain & Repository
๐ฆ Domain Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.kscodes.springboot.reactive.model; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document public class Product { @Id private String id; private String name; private double price; // Getters and Setters } |
๐๏ธ Reactive Repository
1 2 3 4 5 6 7 8 9 10 |
package com.kscodes.springboot.reactive.repository; import org.springframework.data.mongodb.repository.ReactiveMongoRepository; import com.kscodes.springboot.reactive.model.Product; public interface ProductRepository extends ReactiveMongoRepository<Product, String> { } |
๐ฅ๏ธ 4. Building the Reactive REST 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.repository.ProductRepository; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @RestController @RequestMapping("/products") public class ProductController { private final ProductRepository repository; public ProductController(ProductRepository repository) { this.repository = repository; } @GetMapping public Flux<Product> getAllProducts() { return repository.findAll(); } @GetMapping("/{id}") public Mono<Product> getProductById(@PathVariable String id) { return repository.findById(id); } @PostMapping public Mono<Product> createProduct(@RequestBody Product product) { return repository.save(product); } @DeleteMapping("/{id}") public Mono<Void> deleteProduct(@PathVariable String id) { return repository.deleteById(id); } } |
โ ๏ธ 5. Handling Errors in Reactive APIs
Add a Global Error Handler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.kscodes.springboot.reactive.config; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Mono; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public Mono<String> handleException(Exception ex) { return Mono.just("Error: " + ex.getMessage()); } } |
๐งช 6. Testing the Reactive Endpoints
You can use WebTestClient:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@WebFluxTest class ProductControllerTest { @Autowired private WebTestClient webClient; @Test void testGetAllProducts() { webClient.get() .uri("/products") .exchange() .expectStatus().isOk() .expectBodyList(Product.class); } } |
โ 7. Final Thoughts
Building Reactive REST APIs with Spring WebFlux is ideal for high-throughput and event-driven systems. It reduces resource usage and improves responsiveness under load.
Next up: Using Mono and Flux in Reactive APIs