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

Salad Lam - Jun 28 - - Dev Community

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/
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player