Class AbstractHandlerExceptionResolver
- All Implemented Interfaces:
Ordered, HandlerExceptionResolver
- Direct Known Subclasses:
AbstractHandlerMethodExceptionResolver, DefaultHandlerExceptionResolver, ResponseStatusExceptionResolver, SimpleMappingExceptionResolver
HandlerExceptionResolver implementations.
Supports mapped handlers and
handler classes that the resolver
should be applied to and implements the Ordered interface.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Sam Brannen
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogLogger available to subclasses.Fields inherited from interface Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMappedHandlerClass(Class<?> mappedHandlerClass) Alternative tosetMappedHandlerClasses(Class[]).protected StringbuildLogMessage(Exception ex, jakarta.servlet.http.HttpServletRequest request) Build a log message for the given exception, occurred during processing the given request.protected abstract @Nullable ModelAndViewdoResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Actually resolve the given exception that got thrown during handler execution, returning aModelAndViewthat represents a specific error page if appropriate.Return theconfiguredmapped handler classes.intgetOrder()Get the order value of this object.protected booleanWhether there are any handler mappings registered viasetMappedHandlers(Set),setMappedHandlerClasses(Class[]), orsetMappedHandlerPredicate(Predicate).protected voidlogException(Exception ex, jakarta.servlet.http.HttpServletRequest request) Log the given exception at warn level, provided that warn logging has been activated through the"warnLogCategory"property.protected voidprepareResponse(Exception ex, jakarta.servlet.http.HttpServletResponse response) Prepare the response for the exceptional case.protected voidpreventCaching(jakarta.servlet.http.HttpServletResponse response) Prevents the response from being cached, through setting corresponding HTTPCache-Control: no-storeheader.resolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Check whether this resolver is supposed to apply (i.e.voidsetMappedHandlerClasses(Class<?>... mappedHandlerClasses) Specify the set of classes that this exception resolver should apply to.voidsetMappedHandlerPredicate(Predicate<Object> predicate) Use aPredicateto determine which handlers this exception resolver applies to, including when the request was not mapped in which case the handler isnull.voidsetMappedHandlers(Set<?> mappedHandlers) Specify the set of handlers that this exception resolver should apply to.voidsetOrder(int order) voidsetPreventResponseCaching(boolean preventResponseCaching) Specify whether to prevent HTTP response caching for any view resolved by this exception resolver.voidsetWarnLogCategory(String loggerName) Set the log category for warn logging.protected booleanshouldApplyTo(jakarta.servlet.http.HttpServletRequest request, @Nullable Object handler) Check whether this resolver is supposed to apply to the given handler.
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerLogger available to subclasses.
-
-
Constructor Details
-
AbstractHandlerExceptionResolver
public AbstractHandlerExceptionResolver()
-
-
Method Details
-
setOrder
public void setOrder(int order) -
getOrder
public int getOrder()Description copied from interface:OrderedGet 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-startupvalues).Same order values will result in arbitrary sort positions for the affected objects.
-
setMappedHandlerPredicate
Use aPredicateto determine which handlers this exception resolver applies to, including when the request was not mapped in which case the handler isnull.If no handler predicate, nor handlers, nor handler classes are set, the exception resolver applies to all handlers.
- Since:
- 6.1.2
-
setMappedHandlers
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:
-
setMappedHandlerClasses
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:
-
addMappedHandlerClass
Alternative tosetMappedHandlerClasses(Class[]).- Since:
- 6.1
-
getMappedHandlerClasses
Return theconfiguredmapped handler classes. -
setWarnLogCategory
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. Ifnullor empty String is passed, warn logging is turned off.By default there is no warn logging although subclasses like
DefaultHandlerExceptionResolvercan change that default. Specify this setting to activate warn logging into a specific category. Alternatively, override thelogException(Exception, HttpServletRequest)method for custom logging.- See Also:
-
setPreventResponseCaching
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 totruein order to automatically generate HTTP response headers that suppress response caching. -
resolveException
public @Nullable ModelAndView resolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) 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 thedoResolveException(HttpServletRequest, HttpServletResponse, Object, Exception)template method.- Specified by:
resolveExceptionin interfaceHandlerExceptionResolver- Parameters:
request- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif 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
ModelAndViewto forward to, ornullfor default processing in the resolution chain
-
shouldApplyTo
protected boolean shouldApplyTo(jakarta.servlet.http.HttpServletRequest request, @Nullable 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- current HTTP requesthandler- the executed handler, ornullif 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:
-
hasHandlerMappings
protected boolean hasHandlerMappings()Whether there are any handler mappings registered viasetMappedHandlers(Set),setMappedHandlerClasses(Class[]), orsetMappedHandlerPredicate(Predicate).- Since:
- 5.3
-
logException
Log the given exception at warn level, provided that warn logging has been activated through the"warnLogCategory"property.Calls
buildLogMessage(Exception, HttpServletRequest)in order to determine the concrete message to log.- Parameters:
ex- the exception that got thrown during handler executionrequest- current HTTP request (useful for obtaining metadata)- See Also:
-
buildLogMessage
Build a log message for the given exception, occurred during processing the given request.- Parameters:
ex- the exception that got thrown during handler executionrequest- current HTTP request (useful for obtaining metadata)- Returns:
- the log message to use
-
prepareResponse
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- the exception that got thrown during handler executionresponse- current HTTP response- See Also:
-
preventCaching
protected void preventCaching(jakarta.servlet.http.HttpServletResponse response) Prevents the response from being cached, through setting corresponding HTTPCache-Control: no-storeheader.- Parameters:
response- current HTTP response
-
doResolveException
protected abstract @Nullable ModelAndView doResolveException(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, @Nullable Object handler, Exception ex) Actually resolve the given exception that got thrown during handler execution, returning aModelAndViewthat 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- current HTTP requestresponse- current HTTP responsehandler- the executed handler, ornullif 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
ModelAndViewto forward to, ornullfor default processing in the resolution chain
-