Class DefaultHandlerExceptionResolver
- All Implemented Interfaces:
Ordered
,HandlerExceptionResolver
HandlerExceptionResolver
interface, resolving standard Spring MVC exceptions and translating them to corresponding
HTTP status codes.
This exception resolver is enabled by default in the common Spring
DispatcherServlet
.
Supported Exceptions
Exception | HTTP Status Code |
---|---|
HttpRequestMethodNotSupportedException |
405 (SC_METHOD_NOT_ALLOWED) |
HttpMediaTypeNotSupportedException |
415 (SC_UNSUPPORTED_MEDIA_TYPE) |
HttpMediaTypeNotAcceptableException |
406 (SC_NOT_ACCEPTABLE) |
MissingPathVariableException |
500 (SC_INTERNAL_SERVER_ERROR) |
MissingServletRequestParameterException |
400 (SC_BAD_REQUEST) |
MissingServletRequestPartException |
400 (SC_BAD_REQUEST) |
ServletRequestBindingException |
400 (SC_BAD_REQUEST) |
ConversionNotSupportedException |
500 (SC_INTERNAL_SERVER_ERROR) |
TypeMismatchException |
400 (SC_BAD_REQUEST) |
HttpMessageNotReadableException |
400 (SC_BAD_REQUEST) |
HttpMessageNotWritableException |
500 (SC_INTERNAL_SERVER_ERROR) |
MethodArgumentNotValidException |
400 (SC_BAD_REQUEST) |
BindException |
400 (SC_BAD_REQUEST) |
NoHandlerFoundException |
404 (SC_NOT_FOUND) |
AsyncRequestTimeoutException |
503 (SC_SERVICE_UNAVAILABLE) |
- Since:
- 3.0
- Author:
- Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Log category to use when no mapped handler is found for a request.protected static final Log
Additional logger to use when no mapped handler is found for a request.Fields inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
logger
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ModelAndView
doResolveException
(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) Actually resolve the given exception that got thrown during handler execution, returning aModelAndView
that represents a specific error page if appropriate.protected ModelAndView
handleAsyncRequestTimeoutException
(AsyncRequestTimeoutException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where an async request timed out.protected ModelAndView
handleBindException
(BindException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Deprecated, for removal: This API element is subject to removal in a future version.protected ModelAndView
handleConversionNotSupported
(ConversionNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case when aWebDataBinder
conversion cannot occur.protected ModelAndView
handleErrorResponse
(ErrorResponse errorResponse, HttpServletRequest request, HttpServletResponse response, Object handler) Handle anErrorResponse
exception.protected ModelAndView
handleHttpMediaTypeNotAcceptable
(HttpMediaTypeNotAcceptableException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where no message converters were found that were acceptable for the client (expressed via theAccept
header).protected ModelAndView
handleHttpMediaTypeNotSupported
(HttpMediaTypeNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where no message converters were found for PUT or POSTed content.protected ModelAndView
handleHttpMessageNotReadable
(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where a message converter cannot read from an HTTP request.protected ModelAndView
handleHttpMessageNotWritable
(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where a message converter cannot write to an HTTP request.protected ModelAndView
handleHttpRequestMethodNotSupported
(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where no handler was found for the HTTP method.protected ModelAndView
handleMethodArgumentNotValidException
(MethodArgumentNotValidException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where an argument annotated with@Valid
such as anRequestBody
orRequestPart
argument fails validation.protected ModelAndView
handleMissingPathVariable
(MissingPathVariableException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case when a declared path variable does not match any extracted URI variable.protected ModelAndView
handleMissingServletRequestParameter
(MissingServletRequestParameterException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case when a required parameter is missing.protected ModelAndView
handleMissingServletRequestPartException
(MissingServletRequestPartException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where an @RequestPart, aMultipartFile
, or ajakarta.servlet.http.Part
argument is required but is missing.protected ModelAndView
handleNoHandlerFoundException
(NoHandlerFoundException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case where no handler was found during the dispatch.protected ModelAndView
handleServletRequestBindingException
(ServletRequestBindingException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case when an unrecoverable binding exception occurs - e.g.protected ModelAndView
handleTypeMismatch
(TypeMismatchException ex, HttpServletRequest request, HttpServletResponse response, Object handler) Handle the case when aWebDataBinder
conversion error occurs.protected void
sendServerError
(Exception ex, HttpServletRequest request, HttpServletResponse response) Invoked to send a server error.Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
buildLogMessage, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo
-
Field Details
-
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- See Also:
-
pageNotFoundLogger
Additional logger to use when no mapped handler is found for a request.- See Also:
-
-
Constructor Details
-
DefaultHandlerExceptionResolver
public DefaultHandlerExceptionResolver()Sets the order toOrdered.LOWEST_PRECEDENCE
.
-
-
Method Details
-
doResolveException
@Nullable protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex) Description copied from class:AbstractHandlerExceptionResolver
Actually resolve the given exception that got thrown during handler execution, returning aModelAndView
that represents a specific error page if appropriate.May be overridden in subclasses, in order to apply specific exception checks. Note that this template method will be invoked after checking whether this resolved applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.
- Specified by:
doResolveException
in classAbstractHandlerExceptionResolver
- Parameters:
request
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, ornull
if none chosen at the time of the exception (for example, if multipart resolution failed)ex
- the exception that got thrown during handler execution- Returns:
- a corresponding
ModelAndView
to forward to, ornull
for default processing in the resolution chain
-
handleHttpRequestMethodNotSupported
@Nullable protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where no handler was found for the HTTP method.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the HttpRequestMethodNotSupportedException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, ornull
if none chosen at the time of the exception (for example, if multipart resolution failed)- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleHttpMediaTypeNotSupported
@Nullable protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where no message converters were found for PUT or POSTed content.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the HttpMediaTypeNotSupportedException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleHttpMediaTypeNotAcceptable
@Nullable protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where no message converters were found that were acceptable for the client (expressed via theAccept
header).The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the HttpMediaTypeNotAcceptableException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleMissingPathVariable
@Nullable protected ModelAndView handleMissingPathVariable(MissingPathVariableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case when a declared path variable does not match any extracted URI variable.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the MissingPathVariableException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
- Since:
- 4.2
-
handleMissingServletRequestParameter
@Nullable protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case when a required parameter is missing.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the MissingServletRequestParameterException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleMissingServletRequestPartException
@Nullable protected ModelAndView handleMissingServletRequestPartException(MissingServletRequestPartException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where an @RequestPart, aMultipartFile
, or ajakarta.servlet.http.Part
argument is required but is missing.By default, an HTTP 400 error is sent back to the client.
- Parameters:
request
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleServletRequestBindingException
@Nullable protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case when an unrecoverable binding exception occurs - e.g. required header, required cookie.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the exception to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleMethodArgumentNotValidException
@Nullable protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where an argument annotated with@Valid
such as anRequestBody
orRequestPart
argument fails validation.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
request
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleNoHandlerFoundException
@Nullable protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where no handler was found during the dispatch.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- the NoHandlerFoundException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, ornull
if none chosen at the time of the exception (for example, if multipart resolution failed)- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
- Since:
- 4.0
-
handleAsyncRequestTimeoutException
@Nullable protected ModelAndView handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where an async request timed out.The default implementation returns
null
in which case the exception is handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
.- Parameters:
ex
- theAsyncRequestTimeoutException
to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler, ornull
if none chosen at the time of the exception (for example, if multipart resolution failed)- Returns:
- an empty
ModelAndView
indicating the exception was handled, ornull
indicating the exception should be handled inhandleErrorResponse(org.springframework.web.ErrorResponse, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse, java.lang.Object)
- Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
- Since:
- 4.2.8
-
handleErrorResponse
protected ModelAndView handleErrorResponse(ErrorResponse errorResponse, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle anErrorResponse
exception.The default implementation sets status and the headers of the response to those obtained from the
ErrorResponse
. If available, theProblemDetail.getDetail()
is used as the message forHttpServletResponse.sendError(int, String)
.- Parameters:
errorResponse
- the exception to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
- Since:
- 6.0
-
handleConversionNotSupported
protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case when aWebDataBinder
conversion cannot occur.The default implementation sends an HTTP 500 error, and returns an empty
ModelAndView
. Alternatively, a fallback view could be chosen, or the ConversionNotSupportedException could be rethrown as-is.- Parameters:
ex
- the ConversionNotSupportedException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleTypeMismatch
protected ModelAndView handleTypeMismatch(TypeMismatchException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case when aWebDataBinder
conversion error occurs.The default implementation sends an HTTP 400 error, and returns an empty
ModelAndView
. Alternatively, a fallback view could be chosen, or the TypeMismatchException could be rethrown as-is.- Parameters:
ex
- the TypeMismatchException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleHttpMessageNotReadable
protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where a message converter cannot read from an HTTP request.The default implementation sends an HTTP 400 error, and returns an empty
ModelAndView
. Alternatively, a fallback view could be chosen, or the HttpMessageNotReadableException could be rethrown as-is.- Parameters:
ex
- the HttpMessageNotReadableException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleHttpMessageNotWritable
protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Handle the case where a message converter cannot write to an HTTP request.The default implementation sends an HTTP 500 error, and returns an empty
ModelAndView
. Alternatively, a fallback view could be chosen, or the HttpMessageNotWritableException could be rethrown as-is.- Parameters:
ex
- the HttpMessageNotWritableException to be handledrequest
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
handleBindException
@Deprecated(since="6.0", forRemoval=true) protected ModelAndView handleBindException(BindException ex, HttpServletRequest request, HttpServletResponse response, @Nullable Object handler) throws IOException Deprecated, for removal: This API element is subject to removal in a future version.as of 6.0 sinceModelAttributeMethodProcessor
now raises theMethodArgumentNotValidException
subclass instead.Handle the case where an @ModelAttribute method argument has binding or validation errors and is not followed by another method argument of typeBindingResult
.By default, an HTTP 400 error is sent back to the client.
- Parameters:
request
- current HTTP requestresponse
- current HTTP responsehandler
- the executed handler- Returns:
- an empty
ModelAndView
indicating the exception was handled - Throws:
IOException
- potentially thrown fromHttpServletResponse.sendError(int, java.lang.String)
-
sendServerError
protected void sendServerError(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException Invoked to send a server error. Sets the status to 500 and also sets the request attribute "jakarta.servlet.error.exception" to the Exception.- Throws:
IOException
-
ModelAttributeMethodProcessor
now raises theMethodArgumentNotValidException
subclass instead.