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

public abstract Class AbstractUrlHandlerMapping

extends AbstractHandlerMapping
implements MatchableHandlerMapping
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.handler.MatchableHandlerMapping, org.springframework.web.servlet.HandlerMapping
Known Direct Subclasses
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping, org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping
Imports
java.util.ArrayList, .Collections, .Comparator, .LinkedHashMap, .List, .Map, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, org.springframework.beans.BeansException, org.springframework.context.ApplicationContext, org.springframework.http.server.RequestPath, org.springframework.lang.Nullable, org.springframework.util.AntPathMatcher, .Assert, .CollectionUtils, .StringUtils, org.springframework.web.filter.ServerHttpObservationFilter, org.springframework.web.servlet.HandlerExecutionChain, .HandlerInterceptor, .HandlerMapping, org.springframework.web.util.ServletRequestPathUtils, .UrlPathHelper, org.springframework.web.util.pattern.PathPattern, .PathPatternParser

Abstract base class for URL-mapped HandlerMapping implementations.

Supports literal matches and pattern matches such as "/test/*", "/test/**", and others. For details on pattern syntax refer to PathPattern when parsed patterns are enabled or see AntPathMatcher otherwise. The syntax is largely the same but the PathPattern syntax is more tailored for web applications, and its implementation is more efficient.

All path patterns are checked in order to find the most exact match for the current request path where the "most exact" is the longest path pattern that matches the current request path.

Authors
Juergen Hoeller, Arjen Poutsma
Since
16.04.2003

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private class

Field Summary

Modifier and TypeField and Description
private final Map<String, Object>
private boolean
private final Map<PathPattern, Object>
private Object
private boolean
Inherited from org.springframework.web.servlet.handler.AbstractHandlerMapping:
mappingsLoggerSUPPRESS_LOGGING_ATTRIBUTE

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the final handler object
buildPathExposingHandler
(Object
the raw handler to expose
rawHandler
,
String bestMatchingPattern, String
the path to expose before executing the handler
pathWithinMapping
,
Map<String, String>
the URI template variables, can be null if no variables found
uriTemplateVariables
)

Build a handler object for the given raw handler, exposing the actual handler, the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, as well as the URI_TEMPLATE_VARIABLES_ATTRIBUTE before executing the handler.

protected void
exposePathWithinMapping(String bestMatchingPattern, String
the path within the current mapping
pathWithinMapping
,
HttpServletRequest
the request to expose the path to
request
)

Expose the path within the current mapping as request attribute.

protected void
exposeUriTemplateVariables(Map<String, String>
the URI template variables
uriTemplateVariables
,
HttpServletRequest
the request to expose the path to
request
)

Expose the URI templates variables as request attribute.

private Object
private String
protected Object

Returns:

the handler instance, or null if none found
getHandlerInternal
(HttpServletRequest
current HTTP request
request
)

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandlerInternal.

Look up a handler for the URL path of the given request.

public final Map<String, Object>
getHandlerMap()

Return the handler mappings as a read-only Map, with the registered path or pattern as key and the handler object (or handler bean name in case of a lazy-init handler), as value.

public final Map<PathPattern, Object>
getPathPatternHandlerMap()

Identical to getHandlerMap() but populated when parsed patterns are enabled; otherwise empty.

public Object
getRootHandler()

Return the root handler for this handler mapping (registered for "/"), or null if none.

protected Object

Returns:

a matching handler, or null if not found
lookupHandler
(RequestPath
the parsed RequestPath
path
,
String
the String lookupPath for checking direct hits
lookupPath
,
HttpServletRequest
current HTTP request
request
)

Look up a handler instance for the given URL path.

protected Object

Returns:

a matching handler, or null if not found
lookupHandler
(String
the path to match patterns against
lookupPath
,
HttpServletRequest
current HTTP request
request
)

Look up a handler instance for the given URL path.

public RequestMatchResult
match(HttpServletRequest
the current request
request
,
String
the pattern to match
pattern
)

Implements org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Determine whether the request matches the given pattern.

protected void
registerHandler(String[]
the URLs that the bean should be mapped to
urlPaths
,
String
the name of the handler bean
beanName
)

Register the specified handler for the given URL paths.

protected void
registerHandler(String
the URL the bean should be mapped to
urlPath
,
Object
the handler instance or handler bean name String (a bean name will automatically be resolved into the corresponding handler bean)
handler
)

Register the specified handler for the given URL path.

public void
setLazyInitHandlers(boolean lazyInitHandlers)

Set whether to lazily initialize handlers.

public void
setPatternParser(PathPatternParser
the parser to use
patternParser
)

Overrides org.springframework.web.servlet.handler.AbstractHandlerMapping.setPatternParser.

Set the PathPatternParser to parse patterns with for URL path matching.

public void
setRootHandler(Object rootHandler)

Set the root handler for this handler mapping, that is, the handler to be registered for the root path ("/").

public void
setUseTrailingSlashMatch(boolean useTrailingSlashMatch)

Whether to match to URLs irrespective of the presence of a trailing slash.

protected boolean
supportsTypeLevelMappings()

Indicates whether this handler mapping support type-level mappings.

public boolean
useTrailingSlashMatch()

Whether to match to URLs irrespective of the presence of a trailing slash.

protected void
validateHandler(Object
the handler object to validate
handler
,
HttpServletRequest
current HTTP request
request
)

Validate the given handler against the current request.

Inherited from org.springframework.web.servlet.handler.AbstractHandlerMapping:
adaptInterceptordetectMappedInterceptorsextendInterceptorsformatMappingNamegetAdaptedInterceptorsgetCorsConfigurationgetCorsConfigurationSourcegetCorsHandlerExecutionChaingetCorsProcessorgetDefaultHandlergetHandlergetHandlerExecutionChaingetMappedInterceptorsgetOrdergetPathMatchergetPatternParsergetUrlPathHelperhasCorsConfigurationSourceinitApplicationContextinitInterceptorsinitLookupPathsetAlwaysUseFullPathsetBeanNamesetCorsConfigurationssetCorsConfigurationSourcesetCorsProcessorsetDefaultHandlersetInterceptorssetOrdersetPathMatchersetRemoveSemicolonContentsetUrlDecodesetUrlPathHelperusesPathPatterns

Field Detail

handlerMapback to summary
private final Map<String, Object> handlerMap
lazyInitHandlersback to summary
private boolean lazyInitHandlers
pathPatternHandlerMapback to summary
private final Map<PathPattern, Object> pathPatternHandlerMap
rootHandlerback to summary
private Object rootHandler
Annotations
@Nullable
useTrailingSlashMatchback to summary
private boolean useTrailingSlashMatch

Constructor Detail

AbstractUrlHandlerMappingback to summary
public AbstractUrlHandlerMapping()

Method Detail

buildPathExposingHandlerback to summary
protected Object buildPathExposingHandler(Object rawHandler, String bestMatchingPattern, String pathWithinMapping, Map<String, String> uriTemplateVariables)

Build a handler object for the given raw handler, exposing the actual handler, the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, as well as the URI_TEMPLATE_VARIABLES_ATTRIBUTE before executing the handler.

The default implementation builds a HandlerExecutionChain with a special interceptor that exposes the path attribute and URI template variables

Parameters
rawHandler:Object

the raw handler to expose

pathWithinMapping:String

the path to expose before executing the handler

uriTemplateVariables:Map<String, String>

the URI template variables, can be null if no variables found

Returns:Object

the final handler object

exposePathWithinMappingback to summary
protected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request)

Expose the path within the current mapping as request attribute.

Parameters
pathWithinMapping:String

the path within the current mapping

request:HttpServletRequest

the request to expose the path to

See Also
PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
exposeUriTemplateVariablesback to summary
protected void exposeUriTemplateVariables(Map<String, String> uriTemplateVariables, HttpServletRequest request)

Expose the URI templates variables as request attribute.

Parameters
uriTemplateVariables:Map<String, String>

the URI template variables

request:HttpServletRequest

the request to expose the path to

See Also
PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
getDirectMatchback to summary
private Object getDirectMatch(String urlPath, HttpServletRequest request) throws Exception
Annotations
@Nullable
getHandlerDescriptionback to summary
private String getHandlerDescription(Object handler)
getHandlerInternalback to summary
protected Object getHandlerInternal(HttpServletRequest request) throws Exception

Implements abstract org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandlerInternal.

Look up a handler for the URL path of the given request.

Parameters
request:HttpServletRequest

current HTTP request

Returns:Object

the handler instance, or null if none found

Annotations
@Override
@Nullable
Exceptions
Exception:

Doc from org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandlerInternal.

if there is an internal error

getHandlerMapback to summary
public final Map<String, Object> getHandlerMap()

Return the handler mappings as a read-only Map, with the registered path or pattern as key and the handler object (or handler bean name in case of a lazy-init handler), as value.

See Also
getDefaultHandler()
getPathPatternHandlerMapback to summary
public final Map<PathPattern, Object> getPathPatternHandlerMap()

Identical to getHandlerMap() but populated when parsed patterns are enabled; otherwise empty.

Since
5.3
getRootHandlerback to summary
public Object getRootHandler()

Return the root handler for this handler mapping (registered for "/"), or null if none.

Annotations
@Nullable
lookupHandlerback to summary
protected Object lookupHandler(RequestPath path, String lookupPath, HttpServletRequest request) throws Exception

Look up a handler instance for the given URL path. This method is used when parsed PathPatterns are enabled.

Parameters
path:RequestPath

the parsed RequestPath

lookupPath:String

the String lookupPath for checking direct hits

request:HttpServletRequest

current HTTP request

Returns:Object

a matching handler, or null if not found

Annotations
@Nullable
Since
5.3
lookupHandlerback to summary
protected Object lookupHandler(String lookupPath, HttpServletRequest request) throws Exception

Look up a handler instance for the given URL path. This method is used when String pattern matching with PathMatcher is in use.

Parameters
lookupPath:String

the path to match patterns against

request:HttpServletRequest

current HTTP request

Returns:Object

a matching handler, or null if not found

Annotations
@Nullable
See Also
exposePathWithinMapping, AntPathMatcher
matchback to summary
public RequestMatchResult match(HttpServletRequest request, String pattern)

Implements org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Doc from org.springframework.web.servlet.handler.MatchableHandlerMapping.match.

Determine whether the request matches the given pattern. Use this method when getPatternParser() returns null which means that the HandlerMapping is using String pattern matching.

Parameters
request:HttpServletRequest

the current request

pattern:String

the pattern to match

Returns:RequestMatchResult

the result from request matching, or null if none

Annotations
@Override
@Nullable
registerHandlerback to summary
protected void registerHandler(String[] urlPaths, String beanName) throws BeansException, IllegalStateException

Register the specified handler for the given URL paths.

Parameters
urlPaths:String[]

the URLs that the bean should be mapped to

beanName:String

the name of the handler bean

Exceptions
BeansException:
if the handler couldn't be registered
IllegalStateException:
if there is a conflicting handler registered
registerHandlerback to summary
protected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException

Register the specified handler for the given URL path.

Parameters
urlPath:String

the URL the bean should be mapped to

handler:Object

the handler instance or handler bean name String (a bean name will automatically be resolved into the corresponding handler bean)

Exceptions
BeansException:
if the handler couldn't be registered
IllegalStateException:
if there is a conflicting handler registered
setLazyInitHandlersback to summary
public void setLazyInitHandlers(boolean lazyInitHandlers)

Set whether to lazily initialize handlers. Only applicable to singleton handlers, as prototypes are always lazily initialized. Default is "false", as eager initialization allows for more efficiency through referencing the controller objects directly.

If you want to allow your controllers to be lazily initialized, make them "lazy-init" and set this flag to true. Just making them "lazy-init" will not work, as they are initialized through the references from the handler mapping in this case.

setPatternParserback to summary
public void setPatternParser(PathPatternParser patternParser)

Overrides org.springframework.web.servlet.handler.AbstractHandlerMapping.setPatternParser.

Doc from org.springframework.web.servlet.handler.AbstractHandlerMapping.setPatternParser.

Set the PathPatternParser to parse patterns with for URL path matching. Parsed patterns provide a more modern and efficient alternative to String path matching via AntPathMatcher.

Note: This property is mutually exclusive with the below properties, all of which are not necessary for parsed patterns and are ignored when a PathPatternParser is available:

By default, as of 6.0, this is set to a PathPatternParser instance with default settings and therefore use of parsed patterns is enabled. Set this to null to switch to String path matching via AntPathMatcher instead.

Parameters
patternParser:PathPatternParser

the parser to use

Annotations
@Override
setRootHandlerback to summary
public void setRootHandler(Object rootHandler)

Set the root handler for this handler mapping, that is, the handler to be registered for the root path ("/").

Default is null, indicating no root handler.

setUseTrailingSlashMatchback to summary
public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch)

Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a URL pattern such as "/users" also matches to "/users/".

The default value is false.

Annotations
@Deprecated
since:6.0
supportsTypeLevelMappingsback to summary
protected boolean supportsTypeLevelMappings()

Indicates whether this handler mapping support type-level mappings. Default to false.

useTrailingSlashMatchback to summary
public boolean useTrailingSlashMatch()

Whether to match to URLs irrespective of the presence of a trailing slash.

validateHandlerback to summary
protected void validateHandler(Object handler, HttpServletRequest request) throws Exception

Validate the given handler against the current request.

The default implementation is empty. Can be overridden in subclasses, for example to enforce specific preconditions expressed in URL mappings.

Parameters
handler:Object

the handler object to validate

request:HttpServletRequest

current HTTP request

Exceptions
Exception:
if validation failed
org.springframework.web.servlet.handler back to summary

private Class AbstractUrlHandlerMapping.PathExposingHandlerInterceptor

extends Object
implements HandlerInterceptor
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.PathExposingHandlerInterceptor
All Implemented Interfaces
org.springframework.web.servlet.HandlerInterceptor

Special interceptor for exposing the AbstractUrlHandlerMapping#PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE attribute.
See Also
AbstractUrlHandlerMapping#exposePathWithinMapping

Field Summary

Modifier and TypeField and Description
private final String
private final String

Constructor Summary

AccessConstructor and Description
public
PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWithinMapping)

Method Summary

Modifier and TypeMethod and Description
public boolean
preHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
chosen handler to execute, for type and/or instance evaluation
handler
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bestMatchingPatternback to summary
private final String bestMatchingPattern
pathWithinMappingback to summary
private final String pathWithinMapping

Constructor Detail

PathExposingHandlerInterceptorback to summary
public PathExposingHandlerInterceptor(String bestMatchingPattern, String pathWithinMapping)

Method Detail

preHandleback to summary
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.

Note

special considerations apply for asynchronous request processing. For more details see org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

chosen handler to execute, for type and/or instance evaluation

Returns:boolean

true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

Annotations
@Override
org.springframework.web.servlet.handler back to summary

private Class AbstractUrlHandlerMapping.UriTemplateVariablesHandlerInterceptor

extends Object
implements HandlerInterceptor
Class Inheritance
  • java.lang.Object
  • org.springframework.web.servlet.handler.AbstractUrlHandlerMapping.UriTemplateVariablesHandlerInterceptor
All Implemented Interfaces
org.springframework.web.servlet.HandlerInterceptor

Special interceptor for exposing the AbstractUrlHandlerMapping#URI_TEMPLATE_VARIABLES_ATTRIBUTE attribute.
See Also
AbstractUrlHandlerMapping#exposePathWithinMapping

Field Summary

Modifier and TypeField and Description
private final Map<String, String>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
preHandle(HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
,
Object
chosen handler to execute, for type and/or instance evaluation
handler
)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

uriTemplateVariablesback to summary
private final Map<String, String> uriTemplateVariables

Constructor Detail

UriTemplateVariablesHandlerInterceptorback to summary
public UriTemplateVariablesHandlerInterceptor(Map<String, String> uriTemplateVariables)

Method Detail

preHandleback to summary
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

Overrides default org.springframework.web.servlet.HandlerInterceptor.preHandle.

Doc from org.springframework.web.servlet.HandlerInterceptor.preHandle.

Interception point before the execution of a handler. Called after HandlerMapping determined an appropriate handler object, but before HandlerAdapter invokes the handler.

DispatcherServlet processes a handler in an execution chain, consisting of any number of interceptors, with the handler itself at the end. With this method, each interceptor can decide to abort the execution chain, typically sending an HTTP error or writing a custom response.

Note

special considerations apply for asynchronous request processing. For more details see org.springframework.web.servlet.AsyncHandlerInterceptor.

The default implementation returns true.

Parameters
request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

handler:Object

chosen handler to execute, for type and/or instance evaluation

Returns:boolean

true if the execution chain should proceed with the next interceptor or the handler itself. Else, DispatcherServlet assumes that this interceptor has already dealt with the response itself.

Annotations
@Override