Top Description Fields Constructors Methods
org.springframework.web.servlet.mvc.method.annotation

public Class ExceptionHandlerExceptionResolver

extends AbstractHandlerMethodExceptionResolver
implements ApplicationContextAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.Aware
Imports
java.lang.reflect.Method, .Proxy, java.util.ArrayList, .Collections, .LinkedHashMap, .List, .Map, java.util.concurrent.ConcurrentHashMap, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.aop.support.AopUtils, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContext, .ApplicationContextAware, org.springframework.http.HttpStatusCode, org.springframework.http.converter.ByteArrayHttpMessageConverter, .HttpMessageConverter, .StringHttpMessageConverter, org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter, org.springframework.lang.Nullable, org.springframework.ui.ModelMap, org.springframework.web.accept.ContentNegotiationManager, org.springframework.web.bind.annotation.ControllerAdvice, org.springframework.web.context.request.ServletWebRequest, org.springframework.web.method.ControllerAdviceBean, .HandlerMethod, org.springframework.web.method.annotation.ExceptionHandlerMethodResolver, .MapMethodProcessor, .ModelMethodProcessor, org.springframework.web.method.support.HandlerMethodArgumentResolver, .HandlerMethodArgumentResolverComposite, .HandlerMethodReturnValueHandler, .HandlerMethodReturnValueHandlerComposite, .ModelAndViewContainer, org.springframework.web.servlet.ModelAndView, .View, org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver, org.springframework.web.servlet.mvc.support.RedirectAttributes, org.springframework.web.servlet.resource.ResourceHttpRequestHandler, org.springframework.web.servlet.support.RequestContextUtils, org.springframework.web.util.DisconnectedClientHelper

An AbstractHandlerMethodExceptionResolver that resolves exceptions through @ExceptionHandler methods.

Support for custom argument and return value types can be added via setCustomArgumentResolvers and setCustomReturnValueHandlers. Or alternatively to re-configure all argument and return value types use setArgumentResolvers and setReturnValueHandlers(List).

Authors
Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
Since
3.1

Field Summary

Modifier and TypeField and Description
private ApplicationContext
private HandlerMethodArgumentResolverComposite
private ContentNegotiationManager
private List<HandlerMethodArgumentResolver>
private List<HandlerMethodReturnValueHandler>
private static final String
DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

private static final DisconnectedClientHelper
private final Map<ControllerAdviceBean, ExceptionHandlerMethodResolver>
private final Map<Class<?>, ExceptionHandlerMethodResolver>
private final List<HttpMessageConverter<?>>
private final List<Object>
private HandlerMethodReturnValueHandlerComposite

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

protected ModelAndView
doResolveHandlerMethodException(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
HandlerMethod
the executed handler method, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
handlerMethod
,
Exception
the exception that got thrown during handler execution
exception
)

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

Find an @ExceptionHandler method and invoke it to handle the raised exception.

public ApplicationContext
public HandlerMethodArgumentResolverComposite
getArgumentResolvers()

Return the configured argument resolvers, or possibly null if not initialized yet via afterPropertiesSet().

public ContentNegotiationManager
public List<HandlerMethodArgumentResolver>
getCustomArgumentResolvers()

Return the custom argument resolvers, or null.

public List<HandlerMethodReturnValueHandler>
getCustomReturnValueHandlers()

Return the custom return value handlers, or null.

protected List<HandlerMethodArgumentResolver>
getDefaultArgumentResolvers()

Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided via setCustomArgumentResolvers.

protected List<HandlerMethodReturnValueHandler>
getDefaultReturnValueHandlers()

Return the list of return value handlers to use including built-in and custom handlers provided via setReturnValueHandlers.

public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver>
getExceptionHandlerAdviceCache()

Return an unmodifiable Map with the @ControllerAdvice beans discovered in the ApplicationContext.

protected ServletInvocableHandlerMethod

Returns:

a method to handle the exception, or null if none
getExceptionHandlerMethod
(HandlerMethod
the method where the exception was raised (may be null)
handlerMethod
,
Exception
the raised exception
exception
)

Find an @ExceptionHandler method for the given exception.

public List<HttpMessageConverter<?>>
getMessageConverters()

Return the configured message body converters.

public HandlerMethodReturnValueHandlerComposite
getReturnValueHandlers()

Return the configured handlers, or possibly null if not initialized yet via afterPropertiesSet().

protected boolean
hasGlobalExceptionHandlers()

Overrides org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.hasGlobalExceptionHandlers.

Whether this resolver has global exception handlers, e.g. not declared in the same class as the HandlerMethod that raised the exception and therefore can apply to any handler.

private void
private void
public void
setApplicationContext(ApplicationContext
the ApplicationContext object to be used by this object
applicationContext
)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.

public void
setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)

Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.

public void
setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)

Set the ContentNegotiationManager to use to determine requested media types.

public void
setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)

Provide resolvers for custom argument types.

public void
setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)

Provide handlers for custom return value types.

public void
setMessageConverters(List<HttpMessageConverter<?>> messageConverters)

Set the message body converters to use.

public void
setResponseBodyAdvice(List<ResponseBodyAdvice<?>> responseBodyAdvice)

Add one or more components to be invoked after the execution of a controller method annotated with @ResponseBody or returning ResponseEntity but before the body is written to the response with the selected HttpMessageConverter.

public 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
current HTTP request
request
,
Object
the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
handler
)

Overrides org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.shouldApplyTo.

Checks if the handler is a HandlerMethod and then delegates to the base class implementation of #shouldApplyTo(HttpServletRequest, Object) passing the bean of the HandlerMethod.

Inherited from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver:
doResolveException

Field Detail

applicationContextback to summary
private ApplicationContext applicationContext
Annotations
@Nullable
argumentResolversback to summary
private HandlerMethodArgumentResolverComposite argumentResolvers
Annotations
@Nullable
contentNegotiationManagerback to summary
private ContentNegotiationManager contentNegotiationManager
customArgumentResolversback to summary
private List<HandlerMethodArgumentResolver> customArgumentResolvers
Annotations
@Nullable
customReturnValueHandlersback to summary
private List<HandlerMethodReturnValueHandler> customReturnValueHandlers
Annotations
@Nullable
DISCONNECTED_CLIENT_LOG_CATEGORYback to summary
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY

Log category to use for network failure after a client has gone away.

See Also
DisconnectedClientHelper
disconnectedClientHelperback to summary
private static final DisconnectedClientHelper disconnectedClientHelper
exceptionHandlerAdviceCacheback to summary
private final Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> exceptionHandlerAdviceCache
exceptionHandlerCacheback to summary
private final Map<Class<?>, ExceptionHandlerMethodResolver> exceptionHandlerCache
messageConvertersback to summary
private final List<HttpMessageConverter<?>> messageConverters
responseBodyAdviceback to summary
private final List<Object> responseBodyAdvice
returnValueHandlersback to summary
private HandlerMethodReturnValueHandlerComposite returnValueHandlers
Annotations
@Nullable

Constructor Detail

ExceptionHandlerExceptionResolverback to summary
public ExceptionHandlerExceptionResolver()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, 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.

Annotations
@Override
doResolveHandlerMethodExceptionback to summary
protected ModelAndView doResolveHandlerMethodException(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod, Exception exception)

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

Find an @ExceptionHandler method and invoke it to handle the raised exception.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

current HTTP response

handlerMethod:HandlerMethod

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

the executed handler method, or null if none chosen at the time of the exception (for example, if multipart resolution failed)

exception:Exception

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

the exception that got thrown during handler execution

Returns:ModelAndView

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveHandlerMethodException.

a corresponding ModelAndView to forward to, or null for default processing

Annotations
@Override
@Nullable
getApplicationContextback to summary
public ApplicationContext getApplicationContext()
Annotations
@Nullable
getArgumentResolversback to summary
public HandlerMethodArgumentResolverComposite getArgumentResolvers()

Return the configured argument resolvers, or possibly null if not initialized yet via afterPropertiesSet().

Annotations
@Nullable
getContentNegotiationManagerback to summary
public ContentNegotiationManager getContentNegotiationManager()

Return the configured ContentNegotiationManager.

getCustomArgumentResolversback to summary
public List<HandlerMethodArgumentResolver> getCustomArgumentResolvers()

Return the custom argument resolvers, or null.

Annotations
@Nullable
getCustomReturnValueHandlersback to summary
public List<HandlerMethodReturnValueHandler> getCustomReturnValueHandlers()

Return the custom return value handlers, or null.

Annotations
@Nullable
getDefaultArgumentResolversback to summary
protected List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers()

Return the list of argument resolvers to use including built-in resolvers and custom resolvers provided via setCustomArgumentResolvers.

getDefaultReturnValueHandlersback to summary
protected List<HandlerMethodReturnValueHandler> getDefaultReturnValueHandlers()

Return the list of return value handlers to use including built-in and custom handlers provided via setReturnValueHandlers.

getExceptionHandlerAdviceCacheback to summary
public Map<ControllerAdviceBean, ExceptionHandlerMethodResolver> 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 via afterPropertiesSet().

getExceptionHandlerMethodback to summary
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception)

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:HandlerMethod

the method where the exception was raised (may be null)

exception:Exception

the raised exception

Returns:ServletInvocableHandlerMethod

a method to handle the exception, or null if none

Annotations
@Nullable
getMessageConvertersback to summary
public List<HttpMessageConverter<?>> getMessageConverters()

Return the configured message body converters.

getReturnValueHandlersback to summary
public HandlerMethodReturnValueHandlerComposite getReturnValueHandlers()

Return the configured handlers, or possibly null if not initialized yet via afterPropertiesSet().

Annotations
@Nullable
hasGlobalExceptionHandlersback to summary
protected boolean hasGlobalExceptionHandlers()

Overrides org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.hasGlobalExceptionHandlers.

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.hasGlobalExceptionHandlers.

Whether this resolver has global exception handlers, e.g. not declared in the same class as the HandlerMethod that raised the exception and therefore can apply to any handler.

Annotations
@Override
initExceptionHandlerAdviceCacheback to summary
private void initExceptionHandlerAdviceCache()
initMessageConvertersback to summary
private void initMessageConverters()
setApplicationContextback to summary
public void setApplicationContext(ApplicationContext applicationContext)

Implements org.springframework.context.ApplicationContextAware.setApplicationContext.

Doc from org.springframework.context.ApplicationContextAware.setApplicationContext.

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 org.springframework.beans.factory.InitializingBean#afterPropertiesSet() or a custom init-method. Invoked after ResourceLoaderAware#setResourceLoader, ApplicationEventPublisherAware#setApplicationEventPublisher and MessageSourceAware, if applicable.

Parameters
applicationContext:ApplicationContext

the ApplicationContext object to be used by this object

Annotations
@Override
setArgumentResolversback to summary
public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)

Configure the complete list of supported argument types thus overriding the resolvers that would otherwise be configured by default.

setContentNegotiationManagerback to summary
public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)

Set the ContentNegotiationManager to use to determine requested media types. If not set, the default constructor is used.

setCustomArgumentResolversback to summary
public void setCustomArgumentResolvers(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 use setArgumentResolvers instead.

setCustomReturnValueHandlersback to summary
public void setCustomReturnValueHandlers(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 use setReturnValueHandlers.

setMessageConvertersback to summary
public void setMessageConverters(List<HttpMessageConverter<?>> messageConverters)

Set the message body converters to use.

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

setResponseBodyAdviceback to summary
public void setResponseBodyAdvice(List<ResponseBodyAdvice<?>> responseBodyAdvice)

Add one or more components to be invoked after the execution of a controller method annotated with @ResponseBody or returning ResponseEntity but before the body is written to the response with the selected HttpMessageConverter.

setReturnValueHandlersback to summary
public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers)

Configure the complete list of supported return value types thus overriding handlers that would otherwise be configured by default.

shouldApplyToback to summary
protected boolean shouldApplyTo(HttpServletRequest request, Object handler)

Overrides org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.shouldApplyTo.

Doc from org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.shouldApplyTo.

Checks if the handler is a HandlerMethod and then delegates to the base class implementation of #shouldApplyTo(HttpServletRequest, Object) passing the bean of the HandlerMethod. Otherwise returns false.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.shouldApplyTo.

current HTTP request

handler:Object

Doc from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.shouldApplyTo.

the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)

Returns:boolean

Doc from org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.shouldApplyTo.

whether this resolved should proceed with resolving the exception for the given request and handler

Annotations
@Override