DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Introduction to Domain-Driven Design
  • AI and Cybersecurity Protecting Against Emerging Threats
  • 10 Traits That Separate the Best Devs From the Crowd

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Introduction to Domain-Driven Design
  • AI and Cybersecurity Protecting Against Emerging Threats
  • 10 Traits That Separate the Best Devs From the Crowd
  1. DZone
  2. Coding
  3. Frameworks
  4. Exception Handling in Spring Boot WebFlux Reactive REST Web Services

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.

Sanjay Patel user avatar by
Sanjay Patel
CORE ·
Jul. 31, 18 · Tutorial
Like (5)
Save
Tweet
Share
42.60K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

Spring Framework Spring Boot REST Web Protocols

Published at DZone with permission of Sanjay Patel. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Five Java Books Beginners and Professionals Should Read
  • Introduction to Domain-Driven Design
  • AI and Cybersecurity Protecting Against Emerging Threats
  • 10 Traits That Separate the Best Devs From the Crowd

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: