Class AbstractHandlerMapping
- java.lang.Object
- org.springframework.context.support.ApplicationObjectSupport
- org.springframework.web.context.support.WebApplicationObjectSupport
- org.springframework.web.servlet.handler.AbstractHandlerMapping
- All Implemented Interfaces:
Aware
,BeanNameAware
,ApplicationContextAware
,Ordered
,ServletContextAware
,HandlerMapping
- Direct Known Subclasses:
AbstractHandlerMethodMapping
,AbstractUrlHandlerMapping
,RouterFunctionMapping
public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered, BeanNameAware
Abstract base class forHandlerMapping
implementations. Supports ordering, a default handler, handler interceptors, including handler interceptors mapped by path patterns.Note: This base class does not support exposure of the
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
. Support for this attribute is up to concrete subclasses, typically based on request URL mappings.- Since:
- 07.04.2003
- Author:
- Juergen Hoeller, Rossen Stoyanchev
- See Also:
getHandlerInternal(javax.servlet.http.HttpServletRequest)
,setDefaultHandler(java.lang.Object)
,setAlwaysUseFullPath(boolean)
,setUrlDecode(boolean)
,AntPathMatcher
,setInterceptors(java.lang.Object...)
,HandlerInterceptor
Field Summary
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
Fields inherited from interface org.springframework.web.servlet.HandlerMapping
BEST_MATCHING_HANDLER_ATTRIBUTE, BEST_MATCHING_PATTERN_ATTRIBUTE, INTROSPECT_TYPE_LEVEL_MAPPING, LOOKUP_PATH, MATRIX_VARIABLES_ATTRIBUTE, PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, URI_TEMPLATE_VARIABLES_ATTRIBUTE
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor Summary
Constructors Constructor Description AbstractHandlerMapping()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected HandlerInterceptor
adaptInterceptor(Object interceptor)
Adapt the given interceptor object toHandlerInterceptor
.protected void
detectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors)
Detect beans of typeMappedInterceptor
and add them to the list of mapped interceptors.protected void
extendInterceptors(List<Object> interceptors)
Extension hook that subclasses can override to register additional interceptors, given the configured interceptors (seesetInterceptors(java.lang.Object...)
).protected String
formatMappingName()
protected HandlerInterceptor[]
getAdaptedInterceptors()
Return the adapted interceptors asHandlerInterceptor
array.protected CorsConfiguration
getCorsConfiguration(Object handler, HttpServletRequest request)
Retrieve the CORS configuration for the given handler.protected HandlerExecutionChain
getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, CorsConfiguration config)
Update the HandlerExecutionChain for CORS-related handling.CorsProcessor
getCorsProcessor()
Return the configuredCorsProcessor
.Object
getDefaultHandler()
Return the default handler for this handler mapping, ornull
if none.HandlerExecutionChain
getHandler(HttpServletRequest request)
Look up a handler for the given request, falling back to the default handler if no specific one is found.protected HandlerExecutionChain
getHandlerExecutionChain(Object handler, HttpServletRequest request)
Build aHandlerExecutionChain
for the given handler, including applicable interceptors.protected abstract Object
getHandlerInternal(HttpServletRequest request)
Look up a handler for the given request, returningnull
if no specific one is found.protected MappedInterceptor[]
getMappedInterceptors()
Return all configuredMappedInterceptor
s as an array.int
getOrder()
Get the order value of this object.PathMatcher
getPathMatcher()
Return the PathMatcher implementation to use for matching URL paths against registered URL patterns.UrlPathHelper
getUrlPathHelper()
Return the UrlPathHelper implementation to use for resolution of lookup paths.protected boolean
hasCorsConfigurationSource(Object handler)
Returntrue
if there is aCorsConfigurationSource
for this handler.protected void
initApplicationContext()
Initializes the interceptors.protected void
initInterceptors()
Initialize the specified interceptors adaptingWebRequestInterceptor
s toHandlerInterceptor
.void
setAlwaysUseFullPath(boolean alwaysUseFullPath)
Shortcut to same property on underlyingUrlPathHelper
.void
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.void
setCorsConfigurations(Map<String,CorsConfiguration> corsConfigurations)
Set the "global" CORS configurations based on URL patterns.void
setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource)
Set the "global" CORS configuration source.void
setCorsProcessor(CorsProcessor corsProcessor)
Configure a customCorsProcessor
to use to apply the matchedCorsConfiguration
for a request.void
setDefaultHandler(Object defaultHandler)
Set the default handler for this handler mapping.void
setInterceptors(Object... interceptors)
Set the interceptors to apply for all handlers mapped by this handler mapping.void
setOrder(int order)
Specify the order value for this HandlerMapping bean.void
setPathMatcher(PathMatcher pathMatcher)
Set the PathMatcher implementation to use for matching URL paths against registered URL patterns.void
setRemoveSemicolonContent(boolean removeSemicolonContent)
Shortcut to same property on underlyingUrlPathHelper
.void
setUrlDecode(boolean urlDecode)
Shortcut to same property on underlyingUrlPathHelper
.void
setUrlPathHelper(UrlPathHelper urlPathHelper)
Set the UrlPathHelper to use for resolution of lookup paths.Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
Constructor Detail
AbstractHandlerMapping
public AbstractHandlerMapping()
Method Detail
setDefaultHandler
public void setDefaultHandler(@Nullable Object defaultHandler)
Set the default handler for this handler mapping. This handler will be returned if no specific mapping was found.Default is
null
, indicating no default handler.
getDefaultHandler
@Nullable public Object getDefaultHandler()
Return the default handler for this handler mapping, ornull
if none.
setAlwaysUseFullPath
public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
Shortcut to same property on underlyingUrlPathHelper
.
setUrlDecode
public void setUrlDecode(boolean urlDecode)
Shortcut to same property on underlyingUrlPathHelper
.- See Also:
UrlPathHelper.setUrlDecode(boolean)
setRemoveSemicolonContent
public void setRemoveSemicolonContent(boolean removeSemicolonContent)
Shortcut to same property on underlyingUrlPathHelper
.
setUrlPathHelper
public void setUrlPathHelper(UrlPathHelper urlPathHelper)
Set the UrlPathHelper to use for resolution of lookup paths.Use this to override the default UrlPathHelper with a custom subclass, or to share common UrlPathHelper settings across multiple HandlerMappings and MethodNameResolvers.
getUrlPathHelper
public UrlPathHelper getUrlPathHelper()
Return the UrlPathHelper implementation to use for resolution of lookup paths.
setPathMatcher
public void setPathMatcher(PathMatcher pathMatcher)
Set the PathMatcher implementation to use for matching URL paths against registered URL patterns. Default is AntPathMatcher.- See Also:
AntPathMatcher
getPathMatcher
public PathMatcher getPathMatcher()
Return the PathMatcher implementation to use for matching URL paths against registered URL patterns.
setInterceptors
public void setInterceptors(Object... interceptors)
Set the interceptors to apply for all handlers mapped by this handler mapping.Supported interceptor types are HandlerInterceptor, WebRequestInterceptor, and MappedInterceptor. Mapped interceptors apply only to request URLs that match its path patterns. Mapped interceptor beans are also detected by type during initialization.
- Parameters:
interceptors
- array of handler interceptors- See Also:
adaptInterceptor(java.lang.Object)
,HandlerInterceptor
,WebRequestInterceptor
setCorsConfigurations
public void setCorsConfigurations(Map<String,CorsConfiguration> corsConfigurations)
Set the "global" CORS configurations based on URL patterns. By default the first matching URL pattern is combined with the CORS configuration for the handler, if any.- Since:
- 4.2
- See Also:
setCorsConfigurationSource(CorsConfigurationSource)
setCorsConfigurationSource
public void setCorsConfigurationSource(CorsConfigurationSource corsConfigurationSource)
Set the "global" CORS configuration source. By default the first matching URL pattern is combined with the CORS configuration for the handler, if any.- Since:
- 5.1
- See Also:
setCorsConfigurations(Map)
setCorsProcessor
public void setCorsProcessor(CorsProcessor corsProcessor)
Configure a customCorsProcessor
to use to apply the matchedCorsConfiguration
for a request.By default
DefaultCorsProcessor
is used.- Since:
- 4.2
getCorsProcessor
public CorsProcessor getCorsProcessor()
Return the configuredCorsProcessor
.
setOrder
public void setOrder(int order)
Specify the order value for this HandlerMapping bean.The default value is
Ordered.LOWEST_PRECEDENCE
, meaning non-ordered.- See Also:
Ordered.getOrder()
getOrder
public int getOrder()
Description copied from interface:Ordered
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.
- Specified by:
getOrder
in interfaceOrdered
- Returns:
- the order value
- See Also:
Ordered.HIGHEST_PRECEDENCE
,Ordered.LOWEST_PRECEDENCE
setBeanName
public void setBeanName(String name)
Description copied from interface:BeanNameAware
Set the name of the bean in the bean factory that created this bean.Invoked after population of normal bean properties but before an init callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- Specified by:
setBeanName
in interfaceBeanNameAware
- Parameters:
name
- the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use theBeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.
formatMappingName
protected String formatMappingName()
initApplicationContext
protected void initApplicationContext() throws BeansException
Initializes the interceptors.- Overrides:
initApplicationContext
in classApplicationObjectSupport
- Throws:
ApplicationContextException
- in case of initialization errorsBeansException
- if thrown by ApplicationContext methods- See Also:
extendInterceptors(java.util.List)
,initInterceptors()
extendInterceptors
protected void extendInterceptors(List<Object> interceptors)
Extension hook that subclasses can override to register additional interceptors, given the configured interceptors (seesetInterceptors(java.lang.Object...)
).Will be invoked before
initInterceptors()
adapts the specified interceptors intoHandlerInterceptor
instances.The default implementation is empty.
- Parameters:
interceptors
- the configured interceptor List (nevernull
), allowing to add further interceptors before as well as after the existing interceptors
detectMappedInterceptors
protected void detectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors)
Detect beans of typeMappedInterceptor
and add them to the list of mapped interceptors.This is called in addition to any
MappedInterceptor
s that may have been provided viasetInterceptors(java.lang.Object...)
, by default adding all beans of typeMappedInterceptor
from the current context and its ancestors. Subclasses can override and refine this policy.- Parameters:
mappedInterceptors
- an empty list to add to
initInterceptors
protected void initInterceptors()
Initialize the specified interceptors adaptingWebRequestInterceptor
s toHandlerInterceptor
.
adaptInterceptor
protected HandlerInterceptor adaptInterceptor(Object interceptor)
Adapt the given interceptor object toHandlerInterceptor
.By default, the supported interceptor types are
HandlerInterceptor
andWebRequestInterceptor
. Each givenWebRequestInterceptor
is wrapped withWebRequestHandlerInterceptorAdapter
.- Parameters:
interceptor
- the interceptor- Returns:
- the interceptor downcast or adapted to HandlerInterceptor
- See Also:
HandlerInterceptor
,WebRequestInterceptor
,WebRequestHandlerInterceptorAdapter
getAdaptedInterceptors
@Nullable protected final HandlerInterceptor[] getAdaptedInterceptors()
Return the adapted interceptors asHandlerInterceptor
array.- Returns:
- the array of
HandlerInterceptor
s, ornull
if none
getMappedInterceptors
@Nullable protected final MappedInterceptor[] getMappedInterceptors()
Return all configuredMappedInterceptor
s as an array.- Returns:
- the array of
MappedInterceptor
s, ornull
if none
getHandler
@Nullable public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception
Look up a handler for the given request, falling back to the default handler if no specific one is found.- Specified by:
getHandler
in interfaceHandlerMapping
- Parameters:
request
- current HTTP request- Returns:
- the corresponding handler instance, or the default handler
- Throws:
Exception
- if there is an internal error- See Also:
getHandlerInternal(javax.servlet.http.HttpServletRequest)
getHandlerInternal
@Nullable protected abstract Object getHandlerInternal(HttpServletRequest request) throws Exception
Look up a handler for the given request, returningnull
if no specific one is found. This method is called bygetHandler(javax.servlet.http.HttpServletRequest)
; anull
return value will lead to the default handler, if one is set.On CORS pre-flight requests this method should return a match not for the pre-flight request but for the expected actual request based on the URL path, the HTTP methods from the "Access-Control-Request-Method" header, and the headers from the "Access-Control-Request-Headers" header thus allowing the CORS configuration to be obtained via
getCorsConfiguration(Object, HttpServletRequest)
,Note: This method may also return a pre-built
HandlerExecutionChain
, combining a handler object with dynamically determined interceptors. Statically specified interceptors will get merged into such an existing chain.- Parameters:
request
- current HTTP request- Returns:
- the corresponding handler instance, or
null
if none found - Throws:
Exception
- if there is an internal error
getHandlerExecutionChain
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request)
Build aHandlerExecutionChain
for the given handler, including applicable interceptors.The default implementation builds a standard
HandlerExecutionChain
with the given handler, the common interceptors of the handler mapping, and anyMappedInterceptors
matching to the current request URL. Interceptors are added in the order they were registered. Subclasses may override this in order to extend/rearrange the list of interceptors.NOTE: The passed-in handler object may be a raw handler or a pre-built
HandlerExecutionChain
. This method should handle those two cases explicitly, either building a newHandlerExecutionChain
or extending the existing chain.For simply adding an interceptor in a custom subclass, consider calling
super.getHandlerExecutionChain(handler, request)
and invokingHandlerExecutionChain.addInterceptor(org.springframework.web.servlet.HandlerInterceptor)
on the returned chain object.- Parameters:
handler
- the resolved handler instance (nevernull
)request
- current HTTP request- Returns:
- the HandlerExecutionChain (never
null
) - See Also:
getAdaptedInterceptors()
hasCorsConfigurationSource
protected boolean hasCorsConfigurationSource(Object handler)
Returntrue
if there is aCorsConfigurationSource
for this handler.- Since:
- 5.2
getCorsConfiguration
@Nullable protected CorsConfiguration getCorsConfiguration(Object handler, HttpServletRequest request)
Retrieve the CORS configuration for the given handler.- Parameters:
handler
- the handler to check (nevernull
).request
- the current request.- Returns:
- the CORS configuration for the handler, or
null
if none - Since:
- 4.2
getCorsHandlerExecutionChain
protected HandlerExecutionChain getCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, @Nullable CorsConfiguration config)
Update the HandlerExecutionChain for CORS-related handling.For pre-flight requests, the default implementation replaces the selected handler with a simple HttpRequestHandler that invokes the configured
setCorsProcessor(org.springframework.web.cors.CorsProcessor)
.For actual requests, the default implementation inserts a HandlerInterceptor that makes CORS-related checks and adds CORS headers.
- Parameters:
request
- the current requestchain
- the handler chainconfig
- the applicable CORS configuration (possiblynull
)- Since:
- 4.2