Class ExceptionHandlerExceptionResolver
java.lang.Object
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver
org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.InitializingBean,org.springframework.context.ApplicationContextAware,org.springframework.core.Ordered,HandlerExceptionResolver
public class ExceptionHandlerExceptionResolver
extends AbstractHandlerMethodExceptionResolver
implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.InitializingBean
An
AbstractHandlerMethodExceptionResolver that resolves exceptions
through @ExceptionHandler methods.
Support for custom argument and return value types can be added via
setCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>) and setCustomReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>).
Or alternatively to re-configure all argument and return value types use
setArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>) and setReturnValueHandlers(List).
- Since:
- 3.1
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
-
Field Summary
Fields inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
loggerFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected ModelAndViewdoResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, org.springframework.web.method.HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandlermethod and invoke it to handle the raised exception.org.springframework.context.ApplicationContextorg.springframework.web.method.support.HandlerMethodArgumentResolverCompositeReturn the configured argument resolvers, or possiblynullif not initialized yet viaafterPropertiesSet().org.springframework.web.accept.ContentNegotiationManagerReturn the configuredContentNegotiationManager.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>Return the custom argument resolvers, ornull.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>Return the custom return value handlers, ornull.protected List<org.springframework.web.method.support.HandlerMethodArgumentResolver>Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided viasetCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>).protected List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>Return the list of return value handlers to use including built-in and custom handlers provided viasetReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>).Map<org.springframework.web.method.ControllerAdviceBean,org.springframework.web.method.annotation.ExceptionHandlerMethodResolver> Return an unmodifiable Map with the@ControllerAdvicebeans discovered in the ApplicationContext.protected ServletInvocableHandlerMethodgetExceptionHandlerMethod(org.springframework.web.method.HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandlermethod for the given exception.List<org.springframework.http.converter.HttpMessageConverter<?>>Return the configured message body converters.org.springframework.web.method.support.HandlerMethodReturnValueHandlerCompositeReturn the configured handlers, or possiblynullif not initialized yet viaafterPropertiesSet().protected booleanWhether this resolver has global exception handlers, e.g.voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) voidsetArgumentResolvers(List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers) Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.voidsetContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager) Set theContentNegotiationManagerto use to determine requested media types.voidsetCustomArgumentResolvers(List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers) Provide resolvers for custom argument types.voidsetCustomReturnValueHandlers(List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers) Provide handlers for custom return value types.voidsetMessageConverters(List<org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Set the message body converters to use.voidsetResponseBodyAdvice(List<ResponseBodyAdvice<?>> responseBodyAdvice) Add one or more components to be invoked after the execution of a controller method annotated with@ResponseBodyor returningResponseEntitybut before the body is written to the response with the selectedHttpMessageConverter.voidsetReturnValueHandlers(List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers) Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default.Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver
doResolveException, shouldApplyToMethods inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
buildLogMessage, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory
-
Constructor Details
-
ExceptionHandlerExceptionResolver
public ExceptionHandlerExceptionResolver()
-
-
Method Details
-
setCustomArgumentResolvers
public void setCustomArgumentResolvers(@Nullable List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers) Provide resolvers for custom argument types. Custom resolvers are ordered after built-in ones. To override the built-in support for argument resolution usesetArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>)instead. -
getCustomArgumentResolvers
@Nullable public List<org.springframework.web.method.support.HandlerMethodArgumentResolver> getCustomArgumentResolvers()Return the custom argument resolvers, ornull. -
setArgumentResolvers
public void setArgumentResolvers(@Nullable List<org.springframework.web.method.support.HandlerMethodArgumentResolver> argumentResolvers) Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default. -
getArgumentResolvers
@Nullable public org.springframework.web.method.support.HandlerMethodArgumentResolverComposite getArgumentResolvers()Return the configured argument resolvers, or possiblynullif not initialized yet viaafterPropertiesSet(). -
setCustomReturnValueHandlers
public void setCustomReturnValueHandlers(@Nullable List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers) Provide handlers for custom return value types. Custom handlers are ordered after built-in ones. To override the built-in support for return value handling usesetReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>). -
getCustomReturnValueHandlers
@Nullable public List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()Return the custom return value handlers, ornull. -
setReturnValueHandlers
public void setReturnValueHandlers(@Nullable List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> returnValueHandlers) Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default. -
getReturnValueHandlers
@Nullable public org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite getReturnValueHandlers()Return the configured handlers, or possiblynullif not initialized yet viaafterPropertiesSet(). -
setMessageConverters
public void setMessageConverters(List<org.springframework.http.converter.HttpMessageConverter<?>> messageConverters) Set the message body converters to use.These converters are used to convert from and to HTTP requests and responses.
-
getMessageConverters
Return the configured message body converters. -
setContentNegotiationManager
public void setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager) Set theContentNegotiationManagerto use to determine requested media types. If not set, the default constructor is used. -
getContentNegotiationManager
public org.springframework.web.accept.ContentNegotiationManager getContentNegotiationManager()Return the configuredContentNegotiationManager. -
setResponseBodyAdvice
Add one or more components to be invoked after the execution of a controller method annotated with@ResponseBodyor returningResponseEntitybut before the body is written to the response with the selectedHttpMessageConverter. -
setApplicationContext
public void setApplicationContext(@Nullable org.springframework.context.ApplicationContext applicationContext) - Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware
-
getApplicationContext
@Nullable public org.springframework.context.ApplicationContext getApplicationContext() -
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
getExceptionHandlerAdviceCache
public Map<org.springframework.web.method.ControllerAdviceBean,org.springframework.web.method.annotation.ExceptionHandlerMethodResolver> getExceptionHandlerAdviceCache()Return an unmodifiable Map with the@ControllerAdvicebeans discovered in the ApplicationContext. The returned map will be empty if the method is invoked before the bean has been initialized viaafterPropertiesSet(). -
getDefaultArgumentResolvers
protected List<org.springframework.web.method.support.HandlerMethodArgumentResolver> getDefaultArgumentResolvers()Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided viasetCustomArgumentResolvers(java.util.List<org.springframework.web.method.support.HandlerMethodArgumentResolver>). -
getDefaultReturnValueHandlers
protected List<org.springframework.web.method.support.HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()Return the list of return value handlers to use including built-in and custom handlers provided viasetReturnValueHandlers(java.util.List<org.springframework.web.method.support.HandlerMethodReturnValueHandler>). -
hasGlobalExceptionHandlers
protected boolean hasGlobalExceptionHandlers()Description copied from class:AbstractHandlerMethodExceptionResolverWhether this resolver has global exception handlers, e.g. not declared in the same class as theHandlerMethodthat raised the exception and therefore can apply to any handler.- Overrides:
hasGlobalExceptionHandlersin classAbstractHandlerMethodExceptionResolver
-
doResolveHandlerMethodException
@Nullable protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, @Nullable org.springframework.web.method.HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandlermethod and invoke it to handle the raised exception.- Specified by:
doResolveHandlerMethodExceptionin classAbstractHandlerMethodExceptionResolver- Parameters:
request- current HTTP requestresponse- current HTTP responsehandlerMethod- the executed handler method, ornullif none chosen at the time of the exception (for example, if multipart resolution failed)exception- the exception that got thrown during handler execution- Returns:
- a corresponding ModelAndView to forward to, or
nullfor default processing
-
getExceptionHandlerMethod
@Nullable protected ServletInvocableHandlerMethod getExceptionHandlerMethod(@Nullable org.springframework.web.method.HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandlermethod for the given exception. The default implementation searches methods in the class hierarchy of the controller first and if not found, it continues searching for additional@ExceptionHandlermethods assuming some @ControllerAdvice Spring-managed beans were detected.- Parameters:
handlerMethod- the method where the exception was raised (may benull)exception- the raised exception- Returns:
- a method to handle the exception, or
nullif none
-