Spring WebFlux Rest API Global exception handling
3 min readMay 9, 2022
The purpose of this story is to explain how to implement custom exception handling with Spring WebFlux for a REST API.
Error handling is one of the ways we make sure we handle our failures consistently. The benefit of handling exceptions globally is that we can customize all error output from our API into a single format that all consumers can understand. This will guarantee us easier and faster maintenance.
Prerequisites
This is the list of all the prerequisites for following this story:
- Java 17
- Spring Boot / Starter WebFlux 2.6.7
- Lombok 1.18
- Maven 3.6.3
- Postman
Getting Started
First, let’s create a sample spring reactive API from start.spring.io. In our project, we will expose RESTful APIs with @RestController and RouterFunction.