Skip to content

Improve 404 error handling in Spring MVC #29491

Closed
@rstoyanchev

Description

@rstoyanchev

There are two places in Spring MVC that can call sendError(SC_NOT_FOUND). One is the DispatcherServlet and the other ResourceHttpRequestHandler. This prevents applications from handling 404 errors, and also prevents the possibility for default error handling with an RFC 7807 problem details. By contrast, WebFlux raises ResponseStatusException(NOT_FOUND) for both cases.

The DispatcherServlet can be configured to raise NoHandlerFoundException through its throwExceptionIfNoHandlerFound property, but by default it's false. For ResourceHttpRequestHandler there is no way to customize the behavior.

The goal for this issue is to phase out hard-coded use of sendError and allow consistent 404 handling:

  1. Set the throwExceptionIfNoHandlerFound property to true by default, and deprecate it for eventual removal. This will result in raising NoHandlerFoundException by default.
  2. Update ResourceHttpRequestHandler to raise a similar NoResourceFoundException, and provide default handling in ResponseEntityExceptionHandler as well as in DefaultHandlerExceptionResolver.

This should help to address spring-projects/spring-boot#7653. Boot will also need to update its own throwExceptionIfNoHandlerFound property that enforces a false value by default on DispatcherServlet.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions