org.springframework.web.servlet.mvc.support

## Class DefaultHandlerExceptionResolver

- java.lang.Object
- - org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
  - - org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver

- All Implemented Interfaces:
    Ordered, HandlerExceptionResolver

      public class DefaultHandlerExceptionResolver
      extends AbstractHandlerExceptionResolver

  Default implementation of the `HandlerExceptionResolver` interface that resolves standard Spring exceptions and translates them to corresponding HTTP status codes.

  This exception resolver is enabled by default in the `DispatcherServlet`.

  Since:
    3.0
  Author:
    Arjen Poutsma, Rossen Stoyanchev
  See Also:
    `ResponseEntityExceptionHandler`, `handleNoSuchRequestHandlingMethod(org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleTypeMismatch(org.springframework.beans.TypeMismatchException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleMethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleMissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`, `handleBindException(org.springframework.validation.BindException, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `PAGE_NOT_FOUND_LOG_CATEGORY` Log category to use when no mapped handler is found for a request.
    `protected static Log` | `pageNotFoundLogger` 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

    Constructors
    Constructor and Description
    `DefaultHandlerExceptionResolver()` Sets the order to `Ordered.LOWEST_PRECEDENCE`.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected ModelAndView` | `doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)` Actually resolve the given exception that got thrown during on handler execution, returning a ModelAndView that represents a specific error page if appropriate.
    `protected ModelAndView` | `handleBindException(BindException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where an @ModelAttribute method argument has binding or validation errors and is not followed by another method argument of type `BindingResult`.
    `protected ModelAndView` | `handleConversionNotSupported(ConversionNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case when a `WebDataBinder` conversion cannot occur.
    `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 the `Accept` header.
    `protected ModelAndView` | `handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where no message converters were found for the 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 a HTTP request.
    `protected ModelAndView` | `handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where a message converter cannot write to a HTTP request.
    `protected ModelAndView` | `handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where no request handler method was found for the particular HTTP request method.
    `protected ModelAndView` | `handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where an argument annotated with `@Valid` such as an `RequestBody` or `RequestPart` argument fails validation.
    `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, a `MultipartFile`, or a `javax.servlet.http.Part` argument is required but is missing.
    `protected ModelAndView` | `handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response, Object handler)` Handle the case where no request handler method was found.
    `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 a `WebDataBinder` 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, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory, shouldApplyTo`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### 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`, Constant Field Values

    - #### pageNotFoundLogger

          protected static final Log pageNotFoundLogger

      Additional logger to use when no mapped handler is found for a request.
      See Also:
        `PAGE_NOT_FOUND_LOG_CATEGORY`

  - ### Constructor Detail

    - #### DefaultHandlerExceptionResolver

          public DefaultHandlerExceptionResolver()

      Sets the order to `Ordered.LOWEST_PRECEDENCE`.

  - ### Method Detail

    - #### doResolveException

          protected ModelAndView doResolveException(HttpServletRequest request,
                                        HttpServletResponse response,
                                        Object handler,
                                        Exception ex)

      Description copied from class: `AbstractHandlerExceptionResolver`
      Actually resolve the given exception that got thrown during on 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 resolved 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

    - #### handleNoSuchRequestHandlingMethod

          protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response,
                                                       Object handler)
                                                            throws IOException

      Handle the case where no request handler method was found.

      The default implementation logs a warning, sends an HTTP 404 error, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the NoSuchRequestHandlingMethodException could be rethrown as-is.

      Parameters:
        `ex` - the NoSuchRequestHandlingMethodException 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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleHttpRequestMethodNotSupported

          protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response,
                                                         Object handler)
                                                              throws IOException

      Handle the case where no request handler method was found for the particular HTTP request method.

      The default implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the HttpRequestMethodNotSupportedException could be rethrown as-is.

      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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleHttpMediaTypeNotSupported

          protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                                                     HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     Object handler)
                                                          throws IOException

      Handle the case where no message converters were found for the PUT or POSTed content.

      The default implementation sends an HTTP 415 error, sets the "Accept" header, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is.

      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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleHttpMediaTypeNotAcceptable

          protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex,
                                                      HttpServletRequest request,
                                                      HttpServletResponse response,
                                                      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 sends an HTTP 406 error and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotAcceptableException could be rethrown as-is.

      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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleMissingServletRequestParameter

          protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response,
                                                          Object handler)
                                                               throws IOException

      Handle the case when a required parameter is missing.

      The default implementation sends an HTTP 400 error, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the MissingServletRequestParameterException could be rethrown as-is.

      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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleServletRequestBindingException

          protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex,
                                                          HttpServletRequest request,
                                                          HttpServletResponse response,
                                                          Object handler)
                                                               throws IOException

      Handle the case when an unrecoverable binding exception occurs - e.g. required header, required cookie.

      The default implementation sends an HTTP 400 error, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the exception 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 response.sendError()

    - #### handleConversionNotSupported

          protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  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 TypeMismatchException 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 response.sendError()

    - #### 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 "javax.servlet.error.exception" to the Exception.
      Throws:
        `IOException`

    - #### handleTypeMismatch

          protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
                                        HttpServletRequest request,
                                        HttpServletResponse response,
                                        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 response.sendError()

    - #### handleHttpMessageNotReadable

          protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  Object handler)
                                                       throws IOException

      Handle the case where a message converter cannot read from a HTTP request.

      The default implementation sends an HTTP 400 error, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException 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 response.sendError()

    - #### handleHttpMessageNotWritable

          protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  Object handler)
                                                       throws IOException

      Handle the case where a message converter cannot write to a HTTP request.

      The default implementation sends an HTTP 500 error, and returns an empty `ModelAndView`. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException 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 response.sendError()

    - #### handleMethodArgumentNotValidException

          protected ModelAndView handleMethodArgumentNotValidException(MethodArgumentNotValidException ex,
                                                           HttpServletRequest request,
                                                           HttpServletResponse response,
                                                           Object handler)
                                                                throws IOException

      Handle the case where an argument annotated with `@Valid` such as an `RequestBody` or `RequestPart` argument fails validation. 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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleMissingServletRequestPartException

          protected ModelAndView handleMissingServletRequestPartException(MissingServletRequestPartException ex,
                                                              HttpServletRequest request,
                                                              HttpServletResponse response,
                                                              Object handler)
                                                                   throws IOException

      Handle the case where an @RequestPart, a `MultipartFile`, or a `javax.servlet.http.Part` argument is required but is missing. 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
      Throws:
        `IOException` - potentially thrown from response.sendError()

    - #### handleBindException

          protected ModelAndView handleBindException(BindException ex,
                                         HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler)
                                              throws IOException

      Handle the case where an @ModelAttribute method argument has binding or validation errors and is not followed by another method argument of type `BindingResult`. 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
      Throws:
        `IOException` - potentially thrown from response.sendError()