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

Improve 404 error handling in Spring MVC #29491

Closed
rstoyanchev opened this issue Nov 15, 2022 · 0 comments
Closed

Improve 404 error handling in Spring MVC #29491

rstoyanchev opened this issue Nov 15, 2022 · 0 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@rstoyanchev
Copy link
Contributor

rstoyanchev commented Nov 15, 2022

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.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement labels Nov 15, 2022
@rstoyanchev rstoyanchev added this to the 6.0.x milestone Nov 15, 2022
@rstoyanchev rstoyanchev self-assigned this Nov 15, 2022
@rstoyanchev rstoyanchev modified the milestones: 6.0.x, 6.1.x Dec 9, 2022
@rstoyanchev rstoyanchev modified the milestones: 6.1.x, 6.1.0-M2 Jun 15, 2023
@rstoyanchev rstoyanchev changed the title Simplify handling of 404 errors Raise NoHandlerFoundException consistently to allow handling of 404 errors Jun 15, 2023
@rstoyanchev rstoyanchev changed the title Raise NoHandlerFoundException consistently to allow handling of 404 errors Improve 404 error handling in Spring MVC Jun 19, 2023
snicoll added a commit that referenced this issue Apr 4, 2024
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) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant