Class ExceptionHandlerExceptionResolver
- All Implemented Interfaces:
Aware
,InitializingBean
,ApplicationContextAware
,Ordered
,HandlerExceptionResolver
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, Brian Clozel
-
Field Summary
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected ModelAndView
doResolveHandlerMethodException
(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandler
method and invoke it to handle the raised exception.Return the configured argument resolvers, or possiblynull
if not initialized yet viaafterPropertiesSet()
.Return the configuredContentNegotiationManager
.Return the custom argument resolvers, ornull
.Return the custom return value handlers, ornull
.protected List<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<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>)
.Return theconfigured
ErrorResponse.Interceptor
's.Return an unmodifiable Map with the@ControllerAdvice
beans discovered in the ApplicationContext.protected ServletInvocableHandlerMethod
getExceptionHandlerMethod
(HandlerMethod handlerMethod, Exception exception, ServletWebRequest webRequest) Find an@ExceptionHandler
method for the given exception.Return the configured message body converters.Return the configured handlers, or possiblynull
if not initialized yet viaafterPropertiesSet()
.protected boolean
Whether this resolver has global exception handlers, for example, not declared in the same class as theHandlerMethod
that raised the exception and therefore can apply to any handler.void
setApplicationContext
(ApplicationContext applicationContext) Set the ApplicationContext that this object runs in.void
setArgumentResolvers
(List<HandlerMethodArgumentResolver> argumentResolvers) Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.void
setContentNegotiationManager
(ContentNegotiationManager contentNegotiationManager) Set theContentNegotiationManager
to use to determine requested media types.void
setCustomArgumentResolvers
(List<HandlerMethodArgumentResolver> argumentResolvers) Provide resolvers for custom argument types.void
setCustomReturnValueHandlers
(List<HandlerMethodReturnValueHandler> returnValueHandlers) Provide handlers for custom return value types.void
setErrorResponseInterceptors
(List<ErrorResponse.Interceptor> interceptors) Configure a list ofErrorResponse.Interceptor
's to apply when rendering an RFC 9457ProblemDetail
error response.void
setMessageConverters
(List<HttpMessageConverter<?>> messageConverters) Set the message body converters to use.void
setResponseBodyAdvice
(List<ResponseBodyAdvice<?>> responseBodyAdvice) Add one or more components to be invoked after the execution of a controller method annotated with@ResponseBody
or returningResponseEntity
but before the body is written to the response with the selectedHttpMessageConverter
.void
setReturnValueHandlers
(List<HandlerMethodReturnValueHandler> returnValueHandlers) Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default.protected boolean
shouldApplyTo
(HttpServletRequest request, Object handler) Checks if the handler is aHandlerMethod
or the resolver has global exception handlers and then delegates to the base class implementation of#shouldApplyTo
passing the bean of theHandlerMethod
if necessary.Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver
doResolveException
Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver
addMappedHandlerClass, buildLogMessage, getMappedHandlerClasses, getOrder, hasHandlerMappings, logException, prepareResponse, preventCaching, resolveException, setMappedHandlerClasses, setMappedHandlerPredicate, setMappedHandlers, setOrder, setPreventResponseCaching, setWarnLogCategory
-
Constructor Details
-
ExceptionHandlerExceptionResolver
public ExceptionHandlerExceptionResolver()
-
-
Method Details
-
setCustomArgumentResolvers
public void setCustomArgumentResolvers(@Nullable List<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
Return the custom argument resolvers, ornull
. -
setArgumentResolvers
Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default. -
getArgumentResolvers
Return the configured argument resolvers, or possiblynull
if not initialized yet viaafterPropertiesSet()
. -
setCustomReturnValueHandlers
public void setCustomReturnValueHandlers(@Nullable List<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
Return the custom return value handlers, ornull
. -
setReturnValueHandlers
public void setReturnValueHandlers(@Nullable List<HandlerMethodReturnValueHandler> returnValueHandlers) Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default. -
getReturnValueHandlers
Return the configured handlers, or possiblynull
if not initialized yet viaafterPropertiesSet()
. -
setMessageConverters
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
Set theContentNegotiationManager
to use to determine requested media types. If not set, the default constructor is used. -
getContentNegotiationManager
Return the configuredContentNegotiationManager
. -
setResponseBodyAdvice
Add one or more components to be invoked after the execution of a controller method annotated with@ResponseBody
or returningResponseEntity
but before the body is written to the response with the selectedHttpMessageConverter
. -
setErrorResponseInterceptors
Configure a list ofErrorResponse.Interceptor
's to apply when rendering an RFC 9457ProblemDetail
error response.- Parameters:
interceptors
- the handlers to use- Since:
- 6.2
-
getErrorResponseInterceptors
Return theconfigured
ErrorResponse.Interceptor
's.- Since:
- 6.2
-
setApplicationContext
Description copied from interface:ApplicationContextAware
Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.Invoked after population of normal bean properties but before an init callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method. Invoked afterResourceLoaderAware.setResourceLoader(org.springframework.core.io.ResourceLoader)
,ApplicationEventPublisherAware.setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher)
andMessageSourceAware
, if applicable.- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Parameters:
applicationContext
- the ApplicationContext object to be used by this object- See Also:
-
getApplicationContext
-
afterPropertiesSet
public void afterPropertiesSet()Description copied from interface:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
getExceptionHandlerAdviceCache
Return an unmodifiable Map with the@ControllerAdvice
beans discovered in the ApplicationContext. The returned map will be empty if the method is invoked before the bean has been initialized viaafterPropertiesSet()
. -
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
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:AbstractHandlerMethodExceptionResolver
Whether this resolver has global exception handlers, for example, not declared in the same class as theHandlerMethod
that raised the exception and therefore can apply to any handler.- Overrides:
hasGlobalExceptionHandlers
in classAbstractHandlerMethodExceptionResolver
-
shouldApplyTo
Description copied from class:AbstractHandlerMethodExceptionResolver
Checks if the handler is aHandlerMethod
or the resolver has global exception handlers and then delegates to the base class implementation of#shouldApplyTo
passing the bean of theHandlerMethod
if necessary. Otherwise, returnsfalse
.- Overrides:
shouldApplyTo
in classAbstractHandlerMethodExceptionResolver
- Parameters:
request
- current HTTP requesthandler
- the executed handler, ornull
if none chosen at the time of the exception (for example, if multipart resolution failed)- Returns:
- whether this resolved should proceed with resolving the exception for the given request and handler
- See Also:
-
doResolveHandlerMethodException
@Nullable protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) Find an@ExceptionHandler
method and invoke it to handle the raised exception.- Specified by:
doResolveHandlerMethodException
in classAbstractHandlerMethodExceptionResolver
- Parameters:
request
- current HTTP requestresponse
- current HTTP responsehandlerMethod
- the executed handler method, ornull
if 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
null
for default processing
-
getExceptionHandlerMethod
@Nullable protected ServletInvocableHandlerMethod getExceptionHandlerMethod(@Nullable HandlerMethod handlerMethod, Exception exception, ServletWebRequest webRequest) Find an@ExceptionHandler
method 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@ExceptionHandler
methods assuming some @ControllerAdvice Spring-managed beans were detected.- Parameters:
handlerMethod
- the method where the exception was raised (may benull
)exception
- the raised exceptionwebRequest
- the original web request that resulted in a handler error- Returns:
- a method to handle the exception, or
null
if none
-