DEV Community

Salad Lam
Salad Lam

Posted on

Spring Web MVC 6: Show error of REST response in RFC9457 format

Introduction

In Spring Web MVC 6, to show error of REST response in RFC9457 format is supported out of the box. Following is the example of location not found error message.

404 Not Found

To enable it, just add a line into application.properties.



spring.mvc.problemdetails.enabled=true


Enter fullscreen mode Exit fullscreen mode

So, a problemDetailsExceptionHandler bean (class org.springframework.boot.autoconfigure.web.servlet.ProblemDetailsExceptionHandler) will be created. This bean is for constructing the response JSON from Exception instance which is a child class of jakarta.servlet.ServletException. For example, if no handler can be found of a request, a org.springframework.web.servlet.NoHandlerFoundException will be thrown.

Reference

  1. https://swagger.io/blog/problem-details-rfc9457-doing-api-errors-well/
  2. https://swagger.io/blog/problem-details-rfc9457-api-error-handling/
  3. https://app.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0
  4. https://problems-registry.smartbear.com/

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay