Description
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:
- Set the
throwExceptionIfNoHandlerFound
property totrue
by default, and deprecate it for eventual removal. This will result in raisingNoHandlerFoundException
by default. - Update
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 a false
value by default on DispatcherServlet
.