Class AbstractHandlerMapping
- All Implemented Interfaces:
Aware,BeanNameAware,ApplicationContextAware,Ordered,ServletContextAware,HandlerMapping
- Direct Known Subclasses:
AbstractHandlerMethodMapping,AbstractUrlHandlerMapping,RouterFunctionMapping
HandlerMapping
implementations. Supports ordering, a default handler, and 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, Sam Brannen
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final LogDedicated "hidden" logger for request mappings.Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
loggerFields 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_ATTRIBUTEFields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected HandlerInterceptoradaptInterceptor(Object interceptor) Adapt the given interceptor object toHandlerInterceptor.protected voiddetectMappedInterceptors(List<HandlerInterceptor> mappedInterceptors) Detect beans of typeMappedInterceptorand add them to the list of mapped interceptors.protected voidextendInterceptors(List<Object> interceptors) Extension hook that subclasses can override to register additional interceptors, given the configured interceptors (seesetInterceptors(java.lang.Object...)).protected Stringfinal HandlerInterceptor[]Return all configured interceptors adapted toHandlerInterceptor.protected CorsConfigurationgetCorsConfiguration(Object handler, HttpServletRequest request) Retrieve the CORS configuration for the given handler.Return theconfiguredCorsConfigurationSource, if any.protected HandlerExecutionChaingetCorsHandlerExecutionChain(HttpServletRequest request, HandlerExecutionChain chain, CorsConfiguration config) Update the HandlerExecutionChain for CORS-related handling.Return the configuredCorsProcessor.Return the default handler for this handler mapping, ornullif none.final HandlerExecutionChaingetHandler(HttpServletRequest request) Look up a handler for the given request, falling back to the default handler if no specific one is found.protected HandlerExecutionChaingetHandlerExecutionChain(Object handler, HttpServletRequest request) Build aHandlerExecutionChainfor the given handler, including applicable interceptors.protected abstract ObjectgetHandlerInternal(HttpServletRequest request) Look up a handler for the given request, returningnullif no specific one is found.protected final MappedInterceptor[]Return all configuredMappedInterceptors as an array.intgetOrder()Get the order value of this object.Return theconfiguredPathMatcher.Return theconfiguredPathPatternParser, ornullotherwise which indicates that String pattern matching withAntPathMatcheris enabled instead.Return theconfiguredUrlPathHelper.protected booleanhasCorsConfigurationSource(Object handler) Returntrueif there is aCorsConfigurationSourcefor this handler.protected voidInitializes the interceptors.protected voidInitialize the specified interceptors adaptingWebRequestInterceptors toHandlerInterceptor.protected StringinitLookupPath(HttpServletRequest request) Initialize the path to use for request mapping.voidsetAlwaysUseFullPath(boolean alwaysUseFullPath) Deprecated.voidsetBeanName(String name) Set the name of the bean in the bean factory that created this bean.voidsetCorsConfigurations(Map<String, CorsConfiguration> corsConfigurations) Set "global" CORS configuration mappings.voidSet aCorsConfigurationSourcefor "global" CORS config.voidsetCorsProcessor(CorsProcessor corsProcessor) Configure a customCorsProcessorto use to apply the matchedCorsConfigurationfor a request.voidsetDefaultHandler(Object defaultHandler) Set the default handler for this handler mapping.voidsetInterceptors(Object... interceptors) Set the interceptors to apply for all handlers mapped by this handler mapping.voidsetOrder(int order) Specify the order value for this HandlerMapping bean.voidsetPathMatcher(PathMatcher pathMatcher) Configure the PathMatcher to use.voidsetPatternParser(PathPatternParser patternParser) Set thePathPatternParserto parsepatternswith for URL path matching.voidsetRemoveSemicolonContent(boolean removeSemicolonContent) Deprecated.as of 6.0, in favor of usingsetUrlPathHelper(UrlPathHelper)voidsetUrlDecode(boolean urlDecode) Deprecated.as of 6.0, in favor of usingsetUrlPathHelper(UrlPathHelper)voidsetUrlPathHelper(UrlPathHelper urlPathHelper) Configure the UrlPathHelper to use for resolution of lookup paths.booleanMethods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContextMethods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
-
Field Details
-
mappingsLogger
Dedicated "hidden" logger for request mappings.
-
-
Constructor Details
-
AbstractHandlerMapping
public AbstractHandlerMapping()
-
-
Method Details
-
setDefaultHandler
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
Return the default handler for this handler mapping, ornullif none. -
setPatternParser
Set thePathPatternParserto parsepatternswith for URL path matching. Parsed patterns provide a more modern and efficient alternative to String path matching viaAntPathMatcher.Note: This property is mutually exclusive with the below properties, all of which are not necessary for parsed patterns and are ignored when a
PathPatternParseris available:setAlwaysUseFullPath(boolean)-- parsed patterns always use the full path and consider the servletPath only when a Servlet is mapped by path prefix.setRemoveSemicolonContent(boolean)-- parsed patterns always ignore semicolon content for path matching purposes, but path parameters remain available for use in controllers via@MatrixVariable.setUrlDecode(boolean)-- parsed patterns match one decoded path segment at a time and therefore don't need to decode the full path.setUrlPathHelper(org.springframework.web.util.UrlPathHelper)-- for parsed patterns, the request path is parsed once inDispatcherServletor inServletRequestPathFilterusingServletRequestPathUtilsand cached in a request attribute.setPathMatcher(org.springframework.util.PathMatcher)-- a parsed patterns encapsulates the logic for path matching and does need aPathMatcher.
By default, as of 6.0, this is set to a
PathPatternParserinstance with default settings and therefore use of parsed patterns is enabled. Set this tonullto switch to String path matching viaAntPathMatcherinstead.- Parameters:
patternParser- the parser to use- Since:
- 5.3
-
getPatternParser
Return theconfiguredPathPatternParser, ornullotherwise which indicates that String pattern matching withAntPathMatcheris enabled instead.- Since:
- 5.3
-
setAlwaysUseFullPath
Deprecated.as of 6.0, in favor of usingsetUrlPathHelper(UrlPathHelper)Shortcut to same property on the configuredUrlPathHelper.Note: This property is mutually exclusive with and ignored when
setPatternParser(PathPatternParser)is set. -
setUrlDecode
Deprecated.as of 6.0, in favor of usingsetUrlPathHelper(UrlPathHelper)Shortcut to same property on the underlyingUrlPathHelper.Note: This property is mutually exclusive with and ignored when
setPatternParser(PathPatternParser)is set.- See Also:
-
setRemoveSemicolonContent
Deprecated.as of 6.0, in favor of usingsetUrlPathHelper(UrlPathHelper)Shortcut to same property on the underlyingUrlPathHelper.Note: This property is mutually exclusive with and ignored when
setPatternParser(PathPatternParser)is set. -
setUrlPathHelper
Configure the UrlPathHelper to use for resolution of lookup paths.Note: This property is mutually exclusive with and ignored when
setPatternParser(PathPatternParser)is set. -
getUrlPathHelper
Return theconfiguredUrlPathHelper. -
setPathMatcher
Configure the PathMatcher to use.Note: This property is mutually exclusive with and ignored when
setPatternParser(PathPatternParser)is set.By default this is
AntPathMatcher.- See Also:
-
getPathMatcher
Return theconfiguredPathMatcher. -
setInterceptors
Set the interceptors to apply for all handlers mapped by this handler mapping.Supported interceptor types are
HandlerInterceptor,WebRequestInterceptor, andMappedInterceptor. 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:
-
getAdaptedInterceptors
Return all configured interceptors adapted toHandlerInterceptor.- Returns:
- the array of configured interceptors, or
nullif none are configured; this method also returnsnullif called too early, or more specifically beforeApplicationContextAware.setApplicationContext(org.springframework.context.ApplicationContext).
-
getMappedInterceptors
Return all configuredMappedInterceptors as an array.- Returns:
- the array of
MappedInterceptors, ornullif none
-
setCorsConfigurations
Set "global" CORS configuration mappings. The first matching URL pattern determines theCorsConfigurationto use which is then furthercombinedwith theCorsConfigurationfor the selected handler.This is mutually exclusive with
setCorsConfigurationSource(CorsConfigurationSource).- Since:
- 4.2
- See Also:
-
setCorsConfigurationSource
Set aCorsConfigurationSourcefor "global" CORS config. TheCorsConfigurationdetermined by the source iscombinedwith theCorsConfigurationfor the selected handler.This is mutually exclusive with
setCorsConfigurations(Map).- Since:
- 5.1
- See Also:
-
getCorsConfigurationSource
Return theconfiguredCorsConfigurationSource, if any.- Since:
- 5.3
-
setCorsProcessor
Configure a customCorsProcessorto use to apply the matchedCorsConfigurationfor a request.By default
DefaultCorsProcessoris used.- Since:
- 4.2
-
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:
-
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.
-
setBeanName
Description copied from interface:BeanNameAwareSet 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:
setBeanNamein 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
-
initApplicationContext
Initializes the interceptors.- Overrides:
initApplicationContextin classApplicationObjectSupport- Throws:
ApplicationContextException- in case of initialization errorsBeansException- if thrown by ApplicationContext methods- See Also:
-
extendInterceptors
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 intoHandlerInterceptorinstances.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
Detect beans of typeMappedInterceptorand add them to the list of mapped interceptors.This is called in addition to any
MappedInterceptors that may have been provided viasetInterceptors(java.lang.Object...), by default adding all beans of typeMappedInterceptorfrom 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 adaptingWebRequestInterceptors toHandlerInterceptor. -
adaptInterceptor
Adapt the given interceptor object toHandlerInterceptor.By default, the supported interceptor types are
HandlerInterceptorandWebRequestInterceptor. Each givenWebRequestInterceptoris wrapped withWebRequestHandlerInterceptorAdapter.- Parameters:
interceptor- the interceptor- Returns:
- the interceptor downcast or adapted to HandlerInterceptor
- See Also:
-
usesPathPatterns
public boolean usesPathPatterns()- Specified by:
usesPathPatternsin interfaceHandlerMapping
-
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:
getHandlerin 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
Look up a handler for the given request, returningnullif no specific one is found. This method is called bygetHandler(jakarta.servlet.http.HttpServletRequest); anullreturn 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
nullif none found - Throws:
Exception- if there is an internal error
-
initLookupPath
Initialize the path to use for request mapping.When parsed patterns are
enableda parsedRequestPathis expected to have beenparsedexternally by theDispatcherServletorServletRequestPathFilter.Otherwise for String pattern matching via
PathMatcherthe path isresolvedby this method.- Since:
- 5.3
-
getHandlerExecutionChain
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) Build aHandlerExecutionChainfor the given handler, including applicable interceptors.The default implementation builds a standard
HandlerExecutionChainwith the given handler, the common interceptors of the handler mapping, and anyMappedInterceptorsmatching 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 newHandlerExecutionChainor 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:
-
hasCorsConfigurationSource
Returntrueif there is aCorsConfigurationSourcefor 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
nullif 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
-
setUrlPathHelper(UrlPathHelper)