Exception Handling in Spring Boot WebFlux Reactive REST Web Services
Let's take a look at exception handling in Spring Boot WebFlux reactive REST web services.
Join the DZone community and get the full member experience.
Join For FreeThis is a continuation of our series on exception handling and validation in Spring Boot REST APIs. In earlier posts, we discussed a robust pattern for exception handling and validation in Spring Boot and MVC REST Web Services. Please go through that first if you have not already. To summarize, we recommended coding an ErrorComposer
to compose errors from exception objects, which could be used in a controller advice as well as a custom error controller. The controller advice would catch exceptions bubbling up from your controllers, whereas the error controller would be the ultimate weapon for handling exceptions raised at all levels, e.g. in filters.
However, in WebFlux, Spring Boot provides a WebExceptionHandler
filter instead of the error controller. So, instead of providing your custom error controller and error attributes, you would need to provide a custom WebExceptionHandler and/or ErrorAttributes
. For more details, refer to Spring Boot documentation.
So, in our case, coding a custom ErrorAttributes
bean and using our ErrorComposer
in that would be a nice solution. Spring Boot will recognize that bean and use that in its WebExceptionHandler. We can code it by extending Spring Boot's DefaultErrorAttributes
. A simple version could look as below:
@Component
public class CustomErrorAttributes<T extends Throwable> extends DefaultErrorAttributes {
@Autowired
private ErrorResponseComposer<T> errorResponseComposer;
@Override
public Map<String, Object> getErrorAttributes(ServerRequest request,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(request, includeStackTrace);
addErrorDetails(errorAttributes, request);
return errorAttributes;
}
protected void addErrorDetails(
Map<String, Object> errorAttributes, ServerRequest request) {
Throwable ex = getError(request);
errorAttributes.put("exception", ex.getClass().getSimpleName());
errorResponseComposer.compose((T)ex).ifPresent(errorResponse -> {
if (errorResponse.getMessage() != null)
errorAttributes.put("message", errorResponse.getMessage());
Integer status = errorResponse.getStatus();
if (status != null) {
errorAttributes.put("status", status);
errorAttributes.put("error", errorResponse.getError());
}
if (errorResponse.getErrors() != null)
errorAttributes.put("errors", errorResponse.getErrors());
});
}
}
As you see, it extends Spring Boot's DefaultErrorAttributes and overrides the getErrorAttributes
method, adding the attributes compiled by our ErrorComposer. For an exact implementation, look at Spring Lemon's LemonReactiveErrorAttributes class. If you haven’t heard of Spring Lemon, yo should give it a look. It’s a library encapsulating the sophisticated non-functional code and configuration that’s needed when developing real-world RESTful web services using the Spring framework and Spring Boot.
So, with this, you can handle all exceptions in a WebFlux REST API seamlessly!
Finally, instead of coding the ErrorComposer and the exception handlers yourself, you can just use spring-lemon-exceptions
library. To do so, just include the following in your pom:
<dependencies>
<dependency>
<groupId>com.naturalprogrammer.spring-lemon</groupId>
<artifactId>spring-lemon-exceptions</artifactId>
<version>1.0.0.M6</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>naturalprogrammer</id>
<url>https://naturalprogrammer.github.io/mvn-repository</url>
</repository>
</repositories>
For exact details, refer to the Spring Lemon project.
Published at DZone with permission of Sanjay Patel. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments