Top Description Fields Constructors Methods
org.springframework.web.servlet.handler

public abstract Class AbstractHandlerExceptionResolver

extends Object
implements HandlerExceptionResolver, Ordered
Class Inheritance
All Implemented Interfaces
org.springframework.core.Ordered, org.springframework.web.servlet.HandlerExceptionResolver
Known Direct Subclasses
org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver, org.springframework.web.servlet.handler.SimpleMappingExceptionResolver, org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver, org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
Imports
java.util.Set, java.util.function.Predicate, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.Ordered, org.springframework.core.log.LogFormatUtils, org.springframework.lang.Nullable, org.springframework.util.ObjectUtils, .StringUtils, org.springframework.web.servlet.HandlerExceptionResolver, .ModelAndView

Abstract base class for HandlerExceptionResolver implementations.

Supports mapped handlers and handler classes that the resolver should be applied to and implements the Ordered interface.

Authors
Arjen Poutsma, Juergen Hoeller, Sam Brannen
Since
3.0

Field Summary

Modifier and TypeField and Description
private static final String
protected final Log
logger

Logger available to subclasses.

private Class<?>[]
private Predicate<Object>
private Set<?>
private int
private boolean
private Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addMappedHandlerClass(Class<?> mappedHandlerClass)

Alternative to setMappedHandlerClasses(Class[]).

protected String

Returns:

the log message to use
buildLogMessage
(Exception
the exception that got thrown during handler execution
ex
,
HttpServletRequest
current HTTP request (useful for obtaining metadata)
request
)

Build a log message for the given exception, occurred during processing the given request.

protected abstract ModelAndView

Returns:

a corresponding ModelAndView to forward to, or null for default processing in the resolution chain
doResolveException
(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
handler
,
Exception
the exception that got thrown during handler execution
ex
)

Actually resolve the given exception that got thrown during handler execution, returning a ModelAndView that represents a specific error page if appropriate.

protected Class<?>[]
getMappedHandlerClasses()

Return the configured mapped handler classes.

public int
getOrder()

Implements org.springframework.core.Ordered.getOrder.

Get the order value of this object.

protected boolean
protected void
logException(Exception
the exception that got thrown during handler execution
ex
,
HttpServletRequest
current HTTP request (useful for obtaining metadata)
request
)

Log the given exception at warn level, provided that warn logging has been activated through the "warnLogCategory" property.

protected void
prepareResponse(Exception
the exception that got thrown during handler execution
ex
,
HttpServletResponse
current HTTP response
response
)

Prepare the response for the exceptional case.

protected void
preventCaching(HttpServletResponse
current HTTP response
response
)

Prevents the response from being cached, through setting corresponding HTTP Cache-Control: no-store header.

public ModelAndView
resolveException(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
the executed handler, or null if none chosen at the time of the exception (for example, if multipart resolution failed)
handler
,
Exception
the exception that got thrown during handler execution
ex
)

Implements org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

Check whether this resolver is supposed to apply (i.e. if the supplied handler matches any of the configured handlers or handler classes), and then delegate to the doResolveException template method.

public void
setMappedHandlerClasses(Class<?>... mappedHandlerClasses)

Specify the set of classes that this exception resolver should apply to.

public void
setMappedHandlerPredicate(Predicate<Object> predicate)

Use a Predicate to determine which handlers this exception resolver applies to, including when the request was not mapped in which case the handler is null.

public void
setMappedHandlers(Set<?> mappedHandlers)

Specify the set of handlers that this exception resolver should apply to.

public void
setOrder(int order)

public void
setPreventResponseCaching(boolean preventResponseCaching)

Specify whether to prevent HTTP response caching for any view resolved by this exception resolver.

public void
setWarnLogCategory(String loggerName)

Set the log category for warn logging.

protected boolean

Returns:

whether this resolved should proceed with resolving the exception for the given request and handler
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
)

Check whether this resolver is supposed to apply to the given handler.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HEADER_CACHE_CONTROLback to summary
private static final String HEADER_CACHE_CONTROL
loggerback to summary
protected final Log logger

Logger available to subclasses.

mappedHandlerClassesback to summary
private Class<?>[] mappedHandlerClasses
Annotations
@Nullable
mappedHandlerPredicateback to summary
private Predicate<Object> mappedHandlerPredicate
Annotations
@Nullable
mappedHandlersback to summary
private Set<?> mappedHandlers
Annotations
@Nullable
orderback to summary
private int order
preventResponseCachingback to summary
private boolean preventResponseCaching
warnLoggerback to summary
private Log warnLogger
Annotations
@Nullable

Constructor Detail

AbstractHandlerExceptionResolverback to summary
public AbstractHandlerExceptionResolver()

Method Detail

addMappedHandlerClassback to summary
public void addMappedHandlerClass(Class<?> mappedHandlerClass)

Alternative to setMappedHandlerClasses(Class[]).

Since
6.1
buildLogMessageback to summary
protected String buildLogMessage(Exception ex, HttpServletRequest request)

Build a log message for the given exception, occurred during processing the given request.

Parameters
ex:Exception

the exception that got thrown during handler execution

request:HttpServletRequest

current HTTP request (useful for obtaining metadata)

Returns:String

the log message to use

doResolveExceptionback to summary
protected abstract ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

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 resolver applies ("mappedHandlers" etc), so an implementation may simply proceed with its actual exception handling.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

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

ex:Exception

the exception that got thrown during handler execution

Returns:ModelAndView

a corresponding ModelAndView to forward to, or null for default processing in the resolution chain

Annotations
@Nullable
getMappedHandlerClassesback to summary
protected Class<?>[] getMappedHandlerClasses()

Return the configured mapped handler classes.

Annotations
@Nullable
getOrderback to summary
public int getOrder()

Implements org.springframework.core.Ordered.getOrder.

Doc from org.springframework.core.Ordered.getOrder.

Get the order value of this object.

Higher values are interpreted as lower priority. As a consequence, the object with the lowest value has the highest priority (somewhat analogous to Servlet load-on-startup values).

Same order values will result in arbitrary sort positions for the affected objects.

Returns:int

the order value

Annotations
@Override
hasHandlerMappingsback to summary
protected boolean hasHandlerMappings()

Whether there are any handler mappings registered via setMappedHandlers(Set), setMappedHandlerClasses(Class[]), or setMappedHandlerPredicate(Predicate).

Since
5.3
logExceptionback to summary
protected void logException(Exception ex, HttpServletRequest request)

Log the given exception at warn level, provided that warn logging has been activated through the "warnLogCategory" property.

Calls buildLogMessage in order to determine the concrete message to log.

Parameters
ex:Exception

the exception that got thrown during handler execution

request:HttpServletRequest

current HTTP request (useful for obtaining metadata)

See Also
setWarnLogCategory, buildLogMessage, org.apache.commons.logging.Log#warn(Object, Throwable)
prepareResponseback to summary
protected void prepareResponse(Exception ex, HttpServletResponse response)

Prepare the response for the exceptional case.

The default implementation prevents the response from being cached, if the "preventResponseCaching" property has been set to "true".

Parameters
ex:Exception

the exception that got thrown during handler execution

response:HttpServletResponse

current HTTP response

See Also
preventCaching
preventCachingback to summary
protected void preventCaching(HttpServletResponse response)

Prevents the response from being cached, through setting corresponding HTTP Cache-Control: no-store header.

Parameters
response:HttpServletResponse

current HTTP response

resolveExceptionback to summary
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

Implements org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

Check whether this resolver is supposed to apply (i.e. if the supplied handler matches any of the configured handlers or handler classes), and then delegate to the doResolveException template method.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

current HTTP request

response:HttpServletResponse

Doc from org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

current HTTP response

handler:Object

Doc from org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

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

ex:Exception

Doc from org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

the exception that got thrown during handler execution

Returns:ModelAndView

Doc from org.springframework.web.servlet.HandlerExceptionResolver.resolveException.

a corresponding ModelAndView to forward to, or null for default processing in the resolution chain

Annotations
@Override
@Nullable
setMappedHandlerClassesback to summary
public void setMappedHandlerClasses(Class<?>... mappedHandlerClasses)

Specify the set of classes that this exception resolver should apply to. The resolver will only apply to handlers of the specified types; the specified types may be interfaces or superclasses of handlers as well.

If no handler predicate, nor handlers, nor handler classes are set, the exception resolver applies to all handlers.

See Also
setMappedHandlerPredicate(Predicate)
setMappedHandlerPredicateback to summary
public void setMappedHandlerPredicate(Predicate<Object> predicate)

Use a Predicate to determine which handlers this exception resolver applies to, including when the request was not mapped in which case the handler is null.

If no handler predicate, nor handlers, nor handler classes are set, the exception resolver applies to all handlers.

Since
6.1.2
setMappedHandlersback to summary
public void setMappedHandlers(Set<?> mappedHandlers)

Specify the set of handlers that this exception resolver should apply to.

If no handler predicate, nor handlers, nor handler classes are set, the exception resolver applies to all handlers.

See Also
setMappedHandlerPredicate(Predicate)
setOrderback to summary
public void setOrder(int order)
setPreventResponseCachingback to summary
public void setPreventResponseCaching(boolean preventResponseCaching)

Specify whether to prevent HTTP response caching for any view resolved by this exception resolver.

Default is false. Switch this to true in order to automatically generate HTTP response headers that suppress response caching.

setWarnLogCategoryback to summary
public void setWarnLogCategory(String loggerName)

Set the log category for warn logging. The name will be passed to the underlying logger implementation through Commons Logging, getting interpreted as a log category according to the logger's configuration. If null or empty String is passed, warn logging is turned off.

By default there is no warn logging although subclasses like org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver can change that default. Specify this setting to activate warn logging into a specific category. Alternatively, override the logException method for custom logging.

See Also
org.apache.commons.logging.LogFactory#getLog(String), java.util.logging.Logger#getLogger(String)
shouldApplyToback to summary
protected boolean shouldApplyTo(HttpServletRequest request, Object handler)

Check whether this resolver is supposed to apply to the given handler.

The default implementation checks against the configured handlerPredicate handlers and handler classes, if any.

Parameters
request:HttpServletRequest

current HTTP request

handler:Object

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

Returns:boolean

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

See Also
setMappedHandlers, setMappedHandlerClasses