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



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.

404 Not Found

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

  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/


This content originally appeared on DEV Community and was authored by Salad Lam