Skip to content
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

Closed
mzeijen opened this issue Jul 10, 2023 · 5 comments
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue

Comments

@mzeijen
Copy link

mzeijen commented Jul 10, 2023

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.

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Jul 14, 2023

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 rstoyanchev closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2023
@rstoyanchev rstoyanchev added status: duplicate A duplicate of another issue in: web Issues in web modules (web, webmvc, webflux, websocket) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 14, 2023
@mzeijen
Copy link
Author

mzeijen commented Jul 19, 2023

@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.

@rstoyanchev
Copy link
Contributor

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.

@rstoyanchev
Copy link
Contributor

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.

@mzeijen
Copy link
Author

mzeijen commented Jul 25, 2023

Great! Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants