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 the ResourceHttpRequestHandler is executed and it can’t find a resource then it will call the HttpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND) method. Nowhere down the processing line this will result in a ProblemDetail body response, however.

  • Webflux When the ResourceWebHandler is executed and it can’t find a resource then it will return a Mono with a new ResponseStatusException(HttpStatus.NOT_FOUND) error response. However, at this point, this the framework will not try to handle this exception with the ProblemDetailsExceptionHandler 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.

Comment From: rstoyanchev

Thanks for the report. This was the goal in #29491. If you're able to check and confirm that it works well for you with 6.1 M2 that would be much appreciated. I'm going to close this for now as a duplicate, but feel free to comment further.

Comment From: mzeijen

@rstoyanchev I don't think the problem I indicate here is actually solved. My tests run against the latest snapshot version of Spring Boot 3.2.0, which come with the latest snapshot version for Spring Framework 6.1.0. They don't show the behaviour as I expect.

When the ResourceHttpRequestHandler is used to handle a request, then 404 ProblemDetail response is not returned but instead Spring Boots own error json is returned.

Comment From: rstoyanchev

ResoruceHttpRequestHandler now raises NoResourceFoundException which implements ErrorResponse and is handled by ResponseEntityExceptionHandler. I will need to have look closer at the sample, but in principle this is should address the request here.

Comment From: rstoyanchev

I've had a closer look. The 404 when there is no handler works. The same for a missing static resource doesn't. I've created a follow-up issue #30930 to address that.

Comment From: mzeijen

Great! Thanks :)