org.springframework.web.servlet.mvc.support

Class DefaultHandlerExceptionResolver

  • All Implemented Interfaces:
    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)

    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)

    MissingServletRequestPartException

    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:
    ResponseEntityExceptionHandler
    • Field Detail

      • PAGE_NOT_FOUND_LOG_CATEGORY

        public static final java.lang.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
    • Method Detail

      • doResolveException

        @Nullable
        protected ModelAndView doResolveException(HttpServletRequest request,
                                                            HttpServletResponse response,
                                                            @Nullable
                                                            java.lang.Object handler,
                                                            java.lang.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 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 in the resolution chain
      • handleHttpRequestMethodNotSupported

        protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
                                                                   HttpServletRequest request,
                                                                   HttpServletResponse response,
                                                                   @Nullable
                                                                   java.lang.Object handler)
                                                            throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleHttpMediaTypeNotSupported

        protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                                                               HttpServletRequest request,
                                                               HttpServletResponse response,
                                                               @Nullable
                                                               java.lang.Object handler)
                                                        throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleHttpMediaTypeNotAcceptable

        protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex,
                                                                HttpServletRequest request,
                                                                HttpServletResponse response,
                                                                @Nullable
                                                                java.lang.Object handler)
                                                         throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleMissingPathVariable

        protected ModelAndView handleMissingPathVariable(MissingPathVariableException ex,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response,
                                                         @Nullable
                                                         java.lang.Object handler)
                                                  throws java.io.IOException
        Handle the case when a declared path variable does not match any extracted URI variable.

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

        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
        Throws:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
        Since:
        4.2
      • handleMissingServletRequestParameter

        protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
                                                                    HttpServletRequest request,
                                                                    HttpServletResponse response,
                                                                    @Nullable
                                                                    java.lang.Object handler)
                                                             throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleServletRequestBindingException

        protected ModelAndView handleServletRequestBindingException(ServletRequestBindingException ex,
                                                                    HttpServletRequest request,
                                                                    HttpServletResponse response,
                                                                    @Nullable
                                                                    java.lang.Object handler)
                                                             throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleConversionNotSupported

        protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response,
                                                            @Nullable
                                                            java.lang.Object handler)
                                                     throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleTypeMismatch

        protected ModelAndView handleTypeMismatch(TypeMismatchException ex,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  @Nullable
                                                  java.lang.Object handler)
                                           throws java.io.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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleHttpMessageNotReadable

        protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response,
                                                            @Nullable
                                                            java.lang.Object handler)
                                                     throws java.io.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 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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleHttpMessageNotWritable

        protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
                                                            HttpServletRequest request,
                                                            HttpServletResponse response,
                                                            @Nullable
                                                            java.lang.Object handler)
                                                     throws java.io.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 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:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
      • handleNoHandlerFoundException

        protected ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex,
                                                             HttpServletRequest request,
                                                             HttpServletResponse response,
                                                             @Nullable
                                                             java.lang.Object handler)
                                                      throws java.io.IOException
        Handle the case where no handler was found during the dispatch.

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

        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
        Throws:
        java.io.IOException - potentially thrown from HttpServletResponse.sendError(int, java.lang.String)
        Since:
        4.0
      • sendServerError

        protected void sendServerError(java.lang.Exception ex,
                                       HttpServletRequest request,
                                       HttpServletResponse response)
                                throws java.io.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:
        java.io.IOException