-
Notifications
You must be signed in to change notification settings - Fork 38.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The ResourceHttpRequestHandler and ResourceWebHandler prevent returning 404 Not Found ProblemDetail responses #30847
Comments
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. |
@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 |
|
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. |
Great! Thanks :) |
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 anaccept
header for either JSON, XML or any other data format. Although for atext/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) andResourceWebHandler
(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 404Problemdetail
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 theHttpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND)
method. Nowhere down the processing line this will result in aProblemDetail
body response, however.Webflux
When the
ResourceWebHandler
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 inProblemdetail
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.
The text was updated successfully, but these errors were encountered: