Improve 404 error handling in Spring MVC #29491
Labels
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
There are two places in Spring MVC that can call
sendError(SC_NOT_FOUND)
. One is theDispatcherServlet
and the otherResourceHttpRequestHandler
. 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 raisesResponseStatusException(NOT_FOUND)
for both cases.The
DispatcherServlet
can be configured to raiseNoHandlerFoundException
through itsthrowExceptionIfNoHandlerFound
property, but by default it's false. ForResourceHttpRequestHandler
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:throwExceptionIfNoHandlerFound
property totrue
by default, and deprecate it for eventual removal. This will result in raisingNoHandlerFoundException
by default.ResourceHttpRequestHandler
to raise a similarNoResourceFoundException
, and provide default handling inResponseEntityExceptionHandler
as well as inDefaultHandlerExceptionResolver
.This should help to address spring-projects/spring-boot#7653. Boot will also need to update its own
throwExceptionIfNoHandlerFound
property that enforces afalse
value by default onDispatcherServlet
.The text was updated successfully, but these errors were encountered: