Class DefaultHandlerExceptionResolver

java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
All Implemented Interfaces:
org.springframework.core.Ordered, HandlerExceptionResolver

public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver
The default implementation of the 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)
MethodValidationException
500 (SC_INTERNAL_SERVER_ERROR)
HandlerMethodValidationException
400 (SC_BAD_REQUEST)
NoHandlerFoundException
404 (SC_NOT_FOUND)
NoResourceFoundException
404 (SC_NOT_FOUND)
AsyncRequestTimeoutException
503 (SC_SERVICE_UNAVAILABLE)
AsyncRequestNotUsableException
Not applicable
Since:
3.0
Author:
Arjen Poutsma, Rossen Stoyanchev, Juergen Hoeller
See Also:
  • Field Details

    • PAGE_NOT_FOUND_LOG_CATEGORY

      public static final String PAGE_NOT_FOUND_LOG_CATEGORY
      Log category to use when no mapped handler is found for a request.
      See Also:
    • pageNotFoundLogger

      protected static final org.apache.commons.logging.Log pageNotFoundLogger
      Additional logger to use when no mapped handler is found for a request.
      See Also:
  • Constructor Details

    • DefaultHandlerExceptionResolver

      public DefaultHandlerExceptionResolver()
      Sets the order to Ordered.LOWEST_PRECEDENCE.
  • Method Details

    • doResolveException

      protected @Nullable ModelAndView doResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex)
      Description copied from class: AbstractHandlerExceptionResolver
      Actually resolve the given exception that got thrown during handler execution, returning a ModelAndView 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 resolver applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.

      Specified by:
      doResolveException in class AbstractHandlerExceptionResolver
      Parameters:
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null 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, or null for default processing in the resolution chain
    • handleHttpRequestMethodNotSupported

      protected @Nullable ModelAndView handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the HttpRequestMethodNotSupportedException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleHttpMediaTypeNotSupported

      protected @Nullable ModelAndView handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the HttpMediaTypeNotSupportedException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleHttpMediaTypeNotAcceptable

      protected @Nullable ModelAndView handleHttpMediaTypeNotAcceptable(org.springframework.web.HttpMediaTypeNotAcceptableException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where no message converters were found that were acceptable for the client (expressed via the Accept header).

      The default implementation returns null in which case the exception is handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the HttpMediaTypeNotAcceptableException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleMissingPathVariable

      protected @Nullable ModelAndView handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the MissingPathVariableException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      4.2
    • handleMissingServletRequestParameter

      protected @Nullable ModelAndView handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the MissingServletRequestParameterException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleMissingServletRequestPartException

      protected @Nullable ModelAndView handleMissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where an @RequestPart, a MultipartFile, or a jakarta.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 request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleServletRequestBindingException

      protected @Nullable ModelAndView handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case when an unrecoverable binding exception occurs - for example, required header, required cookie.

      The default implementation returns null in which case the exception is handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the exception to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleMethodArgumentNotValidException

      protected @Nullable ModelAndView handleMethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where an argument annotated with @Valid such as an RequestBody or RequestPart argument fails validation.

      The default implementation returns null in which case the exception is handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleHandlerMethodValidationException

      protected @Nullable ModelAndView handleHandlerMethodValidationException(org.springframework.web.method.annotation.HandlerMethodValidationException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where method validation for a controller method failed.

      The default implementation returns null in which case the exception is handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the exception to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      6.1
    • handleNoHandlerFoundException

      protected @Nullable ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the NoHandlerFoundException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      4.0
    • handleNoResourceFoundException

      protected @Nullable ModelAndView handleNoResourceFoundException(NoResourceFoundException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where no static resource was found.

      The default implementation returns null in which case the exception is handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the NoResourceFoundException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the resource handler
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      6.1
    • handleAsyncRequestTimeoutException

      protected @Nullable ModelAndView handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object).

      Parameters:
      ex - the AsyncRequestTimeoutException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
      Returns:
      an empty ModelAndView indicating the exception was handled, or null indicating the exception should be handled in handleErrorResponse(ErrorResponse, HttpServletRequest, HttpServletResponse, Object)
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      4.2.8
    • handleAsyncRequestNotUsableException

      protected ModelAndView handleAsyncRequestNotUsableException(org.springframework.web.context.request.async.AsyncRequestNotUsableException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler)
      Handle the case of an I/O failure from the ServletOutputStream.

      By default, do nothing since the response is not usable.

      Parameters:
      ex - the AsyncRequestTimeoutException to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
      Returns:
      an empty ModelAndView indicating the exception was handled
      Since:
      5.3.33
    • handleDisconnectedClientException

      protected ModelAndView handleDisconnectedClientException(Exception ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler)
      Handle an Exception that indicates the client has gone away. This is typically an IOException of a specific subtype or with a message specific to the underlying Servlet container. Those are detected through DisconnectedClientHelper.isClientDisconnectedException(Throwable)

      By default, do nothing since the response is not usable.

      Parameters:
      ex - the Exception to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
      Returns:
      an empty ModelAndView indicating the exception was handled
      Since:
      6.2
    • handleErrorResponse

      protected ModelAndView handleErrorResponse(org.springframework.web.ErrorResponse errorResponse, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle an ErrorResponse exception.

      The default implementation sets status and the headers of the response to those obtained from the ErrorResponse. If available, the ProblemDetail.getDetail() is used as the message for HttpServletResponse.sendError(int, String).

      Parameters:
      errorResponse - the exception to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      6.0
    • handleConversionNotSupported

      protected ModelAndView handleConversionNotSupported(org.springframework.beans.ConversionNotSupportedException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case when a WebDataBinder 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 handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleTypeMismatch

      protected ModelAndView handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case when a WebDataBinder 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 handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleHttpMessageNotReadable

      protected ModelAndView handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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 handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleHttpMessageNotWritable

      protected ModelAndView handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where a message converter cannot write to an HTTP response.

      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 handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
    • handleMethodValidationException

      protected ModelAndView handleMethodValidationException(org.springframework.validation.method.MethodValidationException ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler) throws IOException
      Handle the case where method validation failed on a component that is not a web controller, for example, on some underlying service.

      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 exception to be handled
      request - current HTTP request
      response - current HTTP response
      handler - the executed handler
      Returns:
      an empty ModelAndView indicating the exception was handled
      Throws:
      IOException - potentially thrown from HttpServletResponse.sendError(int, String)
      Since:
      6.1
    • sendServerError

      protected void sendServerError(Exception ex, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.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
    • logException

      protected void logException(Exception ex, jakarta.servlet.http.HttpServletRequest request)
      Description copied from class: AbstractHandlerExceptionResolver
      Log the given exception at warn level, provided that warn logging has been activated through the "warnLogCategory" property.

      Calls AbstractHandlerExceptionResolver.buildLogMessage(Exception, HttpServletRequest) in order to determine the concrete message to log.

      Overrides:
      logException in class AbstractHandlerExceptionResolver
      Parameters:
      ex - the exception that got thrown during handler execution
      request - current HTTP request (useful for obtaining metadata)
      See Also: