Description
Given RFC 7807 ProblemDetail support is enabled, then I expect that when a call is done to a URL for which nothing can be found, that a 404 Not Found response is returned with a Problemdetail
body. This would certainly be the case when the request contains an accept
header for either JSON, XML or any other data format. Although for a text/html
the exception could be made that a HTML error page is returned (when available)*.
Currently this is not the behavior that a Spring Boot application shows, but instead it will apply the default error behaviour, as if ProblemDetail support is not enabled. The reason has to do with the b ResourceHttpRequestHandler
(for WebMVC) and ResourceWebHandler
(for Webflux) beans. In Spring Boot those beans are created by default and are setup to handle any requests (path pattern is /**
) that are not handled by any other handler. That is why they will try to handle the request for a non existing resource, and this prevents a 404 Problemdetail
response. The reason is slightly different for WebMVC then for Webflux:
-
WebMVC
When theResourceHttpRequestHandler
is executed and it can’t find a resource then it will call theHttpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND)
method. Nowhere down the processing line this will result in aProblemDetail
body response, however. -
Webflux
When theResourceWebHandler
is executed and it can’t find a resource then it will return a Mono with anew ResponseStatusException(HttpStatus.NOT_FOUND)
error response. However, at this point, this the framework will not try to handle this exception with theProblemDetailsExceptionHandler
anymore.
If the ResourceHttpRequestHandler
or ResourceWebHandler are taken out of the picture (for instance by giving them very specific paths to match on) then all requests for which no handlers can be found will result in Problemdetail
JSON responses. To me this is also inconsistent behaviour by the Spring framework.
I created a test that that shows the behaviour as described above: https://github.com/mzeijen/spring-boot-problem-support/blob/main/src/test/java/com/example/demo/NotFoundTest.java
This class contains nested classes with which you can execute the test in different scenarios. So, for WebMVC or Webflux, and with or without the resource handlers being in play or not.
* Ideally when a user visits an application, with ProblemDetail support enabled, with a browser and asking for HTML, then Spring will return the standard HTML error page instead of returning a ProblemDetail JSON or XML. This would particularly be important for applications that both serve a browser frontend as well as a REST API. But, I can understand that this should be requested in another Github issue. I can do so, if you agree with my thinking.