org.springframework.web.servlet.mvc.annotation
Class AnnotationMethodHandlerExceptionResolver

java.lang.Object
  extended by org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
      extended by org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver
All Implemented Interfaces:
Ordered, HandlerExceptionResolver

public class AnnotationMethodHandlerExceptionResolver
extends AbstractHandlerExceptionResolver

Implementation of the HandlerExceptionResolver interface that handles exceptions through the ExceptionHandler annotation.

This exception resolver is enabled by default in the DispatcherServlet.

Since:
3.0
Author:
Arjen Poutsma, Juergen Hoeller

Field Summary
private  WebArgumentResolver[] customArgumentResolvers
           
private  HttpMessageConverter<?>[] messageConverters
           
 
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
AnnotationMethodHandlerExceptionResolver()
           
 
Method Summary
private  java.lang.Object doInvokeMethod(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object[] args)
           
protected  ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, java.lang.Object handler, java.lang.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.
private  java.lang.reflect.Method findBestExceptionHandlerMethod(java.lang.Object handler, java.lang.Exception thrownException)
          Finds the handler method that matches the thrown exception best.
private  java.lang.reflect.Method getBestMatchingMethod(java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> resolverMethods, java.lang.Exception thrownException)
          Returns the best matching method.
protected  java.util.List<java.lang.Class<? extends java.lang.Throwable>> getHandledExceptions(java.lang.reflect.Method method)
          Returns all the exception classes handled by the given method.
private  ModelAndView getModelAndView(java.lang.reflect.Method handlerMethod, java.lang.Object returnValue, ServletWebRequest webRequest)
           
private  ModelAndView handleResponseBody(java.lang.Object returnValue, ServletWebRequest webRequest)
           
protected  java.lang.Object resolveCommonArgument(MethodParameter methodParameter, NativeWebRequest webRequest, java.lang.Exception thrownException)
          Resolves common method arguments.
private  java.lang.Object[] resolveHandlerArguments(java.lang.reflect.Method handlerMethod, java.lang.Object handler, NativeWebRequest webRequest, java.lang.Exception thrownException)
          Resolves the arguments for the given method.
protected  java.lang.Object resolveStandardArgument(java.lang.Class parameterType, NativeWebRequest webRequest, java.lang.Exception thrownException)
          Resolves standard method arguments.
 void setCustomArgumentResolver(WebArgumentResolver argumentResolver)
          Set a custom ArgumentResolvers to use for special method parameter types.
 void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers)
          Set one or more custom ArgumentResolvers to use for special method parameter types.
 void setMessageConverters(HttpMessageConverter<?>[] messageConverters)
          Set the message body converters to use.
 
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

customArgumentResolvers

private WebArgumentResolver[] customArgumentResolvers

messageConverters

private HttpMessageConverter<?>[] messageConverters
Constructor Detail

AnnotationMethodHandlerExceptionResolver

public AnnotationMethodHandlerExceptionResolver()
Method Detail

setCustomArgumentResolver

public void setCustomArgumentResolver(WebArgumentResolver argumentResolver)
Set a custom ArgumentResolvers to use for special method parameter types.

Such a custom ArgumentResolver will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.


setCustomArgumentResolvers

public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers)
Set one or more custom ArgumentResolvers to use for special method parameter types.

Any such custom ArgumentResolver will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.


setMessageConverters

public void setMessageConverters(HttpMessageConverter<?>[] messageConverters)
Set the message body converters to use.

These converters are used to convert from and to HTTP requests and responses.


doResolveException

protected ModelAndView doResolveException(HttpServletRequest request,
                                          HttpServletResponse response,
                                          java.lang.Object handler,
                                          java.lang.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

findBestExceptionHandlerMethod

private java.lang.reflect.Method findBestExceptionHandlerMethod(java.lang.Object handler,
                                                                java.lang.Exception thrownException)
Finds the handler method that matches the thrown exception best.

Parameters:
handler - the handler object
thrownException - the exception to be handled
Returns:
the best matching method; or null if none is found

getHandledExceptions

protected java.util.List<java.lang.Class<? extends java.lang.Throwable>> getHandledExceptions(java.lang.reflect.Method method)
Returns all the exception classes handled by the given method.

The default implementation looks for exceptions in the annotation, or - if that annotation element is empty - any exceptions listed in the method parameters if the method is annotated with @ExceptionHandler.

Parameters:
method - the method
Returns:
the handled exceptions

getBestMatchingMethod

private java.lang.reflect.Method getBestMatchingMethod(java.util.Map<java.lang.Class<? extends java.lang.Throwable>,java.lang.reflect.Method> resolverMethods,
                                                       java.lang.Exception thrownException)
Returns the best matching method. Uses the DepthComparator.


resolveHandlerArguments

private java.lang.Object[] resolveHandlerArguments(java.lang.reflect.Method handlerMethod,
                                                   java.lang.Object handler,
                                                   NativeWebRequest webRequest,
                                                   java.lang.Exception thrownException)
                                            throws java.lang.Exception
Resolves the arguments for the given method. Delegates to resolveCommonArgument(org.springframework.core.MethodParameter, org.springframework.web.context.request.NativeWebRequest, java.lang.Exception).

Throws:
java.lang.Exception

resolveCommonArgument

protected java.lang.Object resolveCommonArgument(MethodParameter methodParameter,
                                                 NativeWebRequest webRequest,
                                                 java.lang.Exception thrownException)
                                          throws java.lang.Exception
Resolves common method arguments. Delegates to registered argumentResolvers first, then checking resolveStandardArgument(java.lang.Class, org.springframework.web.context.request.NativeWebRequest, java.lang.Exception).

Parameters:
methodParameter - the method parameter
webRequest - the request
thrownException - the exception thrown
Returns:
the argument value, or WebArgumentResolver.UNRESOLVED
Throws:
java.lang.Exception

resolveStandardArgument

protected java.lang.Object resolveStandardArgument(java.lang.Class parameterType,
                                                   NativeWebRequest webRequest,
                                                   java.lang.Exception thrownException)
                                            throws java.lang.Exception
Resolves standard method arguments. The default implementation handles NativeWebRequest, ServletRequest, ServletResponse, HttpSession, Principal, Locale, request InputStream, request Reader, response OutputStream, response Writer, and the given thrownException.

Parameters:
parameterType - the method parameter type
webRequest - the request
thrownException - the exception thrown
Returns:
the argument value, or WebArgumentResolver.UNRESOLVED
Throws:
java.lang.Exception

doInvokeMethod

private java.lang.Object doInvokeMethod(java.lang.reflect.Method method,
                                        java.lang.Object target,
                                        java.lang.Object[] args)
                                 throws java.lang.Exception
Throws:
java.lang.Exception

getModelAndView

private ModelAndView getModelAndView(java.lang.reflect.Method handlerMethod,
                                     java.lang.Object returnValue,
                                     ServletWebRequest webRequest)
                              throws java.lang.Exception
Throws:
java.lang.Exception

handleResponseBody

private ModelAndView handleResponseBody(java.lang.Object returnValue,
                                        ServletWebRequest webRequest)
                                 throws ServletException,
                                        java.io.IOException
Throws:
ServletException
java.io.IOException