Skip to content

The ResourceHttpRequestHandler and ResourceWebHandler prevent returning 404 Not Found ProblemDetail responses #30847

Closed as not planned
@mzeijen

Description

@mzeijen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions