Spring WebFlux Rest API Global exception handling

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.

Global Error Handling

The custom exception system provided in SpringMVC doesn’t work in Spring-WebFlux for the simple reason that the underlying runtime containers are not the same. WebExceptionHandler is the top-level interface to the exception handler of Spring-WebFlux, so it can be traced back to the subclass.

  1. Customize the Global Error Response Attributes
  2. Implement the Global Error Handler
Global exception attribute value class

Conclusion

Well done !!. In this story, We have seen the customized exception handling using Spring Webflux.

References

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store