This content originally appeared on DEV Community and was authored by Salad Lam
Introduction
In Spring Web MVC 6, to show error of REST response in RFC9457 format is support out of the box. Following is the example of location not found error message.
To enable it, just add a line into application.properties.
spring.mvc.problemdetails.enabled=true
So, a problemDetailsExceptionHandler bean (class org.springframework.boot.autoconfigure.web.servlet.ProblemDetailsExceptionHandler) will be created. This bean is for to construct the response JSON from Exception instance which is 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
- https://swagger.io/blog/problem-details-rfc9457-doing-api-errors-well/
- https://swagger.io/blog/problem-details-rfc9457-api-error-handling/
- https://app.swaggerhub.com/domains/smartbear-public/ProblemDetails/1.0.0
- https://problems-registry.smartbear.com/
This content originally appeared on DEV Community and was authored by Salad Lam