public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping implements MatchableHandlerMapping
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.
logger
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
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
AbstractUrlHandlerMapping() |
Modifier and Type | Method and Description |
---|---|
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
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE , as well as
the HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE before executing the handler. |
protected void |
exposePathWithinMapping(String bestMatchingPattern,
String pathWithinMapping,
HttpServletRequest request)
Expose the path within the current mapping as request attribute.
|
protected void |
exposeUriTemplateVariables(Map<String,String> uriTemplateVariables,
HttpServletRequest request)
Expose the URI templates variables as request attribute.
|
protected Object |
getHandlerInternal(HttpServletRequest request)
Look up a handler for the URL path of the given request.
|
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.
|
Map<PathPattern,Object> |
getPathPatternHandlerMap()
Identical to
getHandlerMap() but populated when parsed patterns
are enabled ; otherwise empty. |
Object |
getRootHandler()
Return the root handler for this handler mapping (registered for "/"),
or
null if none. |
protected Object |
lookupHandler(RequestPath path,
String lookupPath,
HttpServletRequest request)
Look up a handler instance for the given URL path.
|
protected Object |
lookupHandler(String lookupPath,
HttpServletRequest request)
Look up a handler instance for the given URL path.
|
RequestMatchResult |
match(HttpServletRequest request,
String pattern)
Determine whether the request matches the given pattern.
|
protected void |
registerHandler(String[] urlPaths,
String beanName)
Register the specified handler for the given URL paths.
|
protected void |
registerHandler(String urlPath,
Object handler)
Register the specified handler for the given URL path.
|
void |
setLazyInitHandlers(boolean lazyInitHandlers)
Set whether to lazily initialize handlers.
|
void |
setPatternParser(PathPatternParser patternParser)
Enable use of pre-parsed
PathPattern s as an alternative to
String pattern matching with AntPathMatcher . |
void |
setRootHandler(Object rootHandler)
Set the root handler for this handler mapping, that is,
the handler to be registered for the root path ("/").
|
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.
|
boolean |
useTrailingSlashMatch()
Whether to match to URLs irrespective of the presence of a trailing slash.
|
protected void |
validateHandler(Object handler,
HttpServletRequest request)
Validate the given handler against the current request.
|
adaptInterceptor, detectMappedInterceptors, extendInterceptors, formatMappingName, getAdaptedInterceptors, getCorsConfiguration, getCorsConfigurationSource, getCorsHandlerExecutionChain, getCorsProcessor, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getPatternParser, getUrlPathHelper, hasCorsConfigurationSource, initApplicationContext, initInterceptors, initLookupPath, setAlwaysUseFullPath, setBeanName, setCorsConfigurations, setCorsConfigurationSource, setCorsProcessor, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setRemoveSemicolonContent, setUrlDecode, setUrlPathHelper, usesPathPatterns
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getPatternParser
getHandler, usesPathPatterns
public void setPatternParser(PathPatternParser patternParser)
AbstractHandlerMapping
PathPattern
s as an alternative to
String pattern matching with AntPathMatcher
. The syntax is
largely the same but the PathPattern
syntax is more tailored for
web applications, and its implementation is more efficient.
This property is mutually exclusive with the following others which are effectively ignored when this is set:
AbstractHandlerMapping.setAlwaysUseFullPath(boolean)
-- PathPatterns
always use the
full path and ignore the servletPath/pathInfo which are decoded and
partially normalized and therefore not comparable against the
requestURI
.
AbstractHandlerMapping.setRemoveSemicolonContent(boolean)
-- PathPatterns
always
ignore semicolon content for path matching purposes, but path parameters
remain available for use in controllers via @MatrixVariable
.
AbstractHandlerMapping.setUrlDecode(boolean)
-- PathPatterns
match one decoded path
segment at a time and never need the full decoded path which can cause
issues due to decoded reserved characters.
AbstractHandlerMapping.setUrlPathHelper(org.springframework.web.util.UrlPathHelper)
-- the request path is pre-parsed globally
by the DispatcherServlet
or by
ServletRequestPathFilter
using ServletRequestPathUtils
and saved
in a request attribute for re-use.
AbstractHandlerMapping.setPathMatcher(org.springframework.util.PathMatcher)
-- patterns are parsed to PathPatterns
and used instead of String matching with PathMatcher
.
By default this is not set.
setPatternParser
in class AbstractHandlerMapping
patternParser
- the parser to usepublic void setRootHandler(@Nullable Object rootHandler)
Default is null
, indicating no root handler.
@Nullable public Object getRootHandler()
null
if none.public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch)
The default value is false
.
public boolean useTrailingSlashMatch()
public void setLazyInitHandlers(boolean lazyInitHandlers)
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.
@Nullable protected Object getHandlerInternal(HttpServletRequest request) throws Exception
getHandlerInternal
in class AbstractHandlerMapping
request
- current HTTP requestnull
if none foundException
- if there is an internal error@Nullable protected Object lookupHandler(RequestPath path, String lookupPath, HttpServletRequest request) throws Exception
PathPattern
s are enabled
.path
- the parsed RequestPathlookupPath
- the String lookupPath for checking direct hitsrequest
- current HTTP requestnull
if not foundException
@Nullable protected Object lookupHandler(String lookupPath, HttpServletRequest request) throws Exception
PathMatcher
is in use.lookupPath
- the path to match patterns againstrequest
- current HTTP requestnull
if not foundException
exposePathWithinMapping(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
,
AntPathMatcher
protected void validateHandler(Object handler, HttpServletRequest request) throws Exception
The default implementation is empty. Can be overridden in subclasses, for example to enforce specific preconditions expressed in URL mappings.
handler
- the handler object to validaterequest
- current HTTP requestException
- if validation failedprotected Object buildPathExposingHandler(Object rawHandler, String bestMatchingPattern, String pathWithinMapping, @Nullable Map<String,String> uriTemplateVariables)
HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
, as well as
the HandlerMapping.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
rawHandler
- the raw handler to exposepathWithinMapping
- the path to expose before executing the handleruriTemplateVariables
- the URI template variables, can be null
if no variables foundprotected void exposePathWithinMapping(String bestMatchingPattern, String pathWithinMapping, HttpServletRequest request)
pathWithinMapping
- the path within the current mappingrequest
- the request to expose the path toHandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
protected void exposeUriTemplateVariables(Map<String,String> uriTemplateVariables, HttpServletRequest request)
uriTemplateVariables
- the URI template variablesrequest
- the request to expose the path toHandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE
@Nullable public RequestMatchResult match(HttpServletRequest request, String pattern)
MatchableHandlerMapping
MatchableHandlerMapping.getPatternParser()
returns null
which means that the
HandlerMapping
is uses String pattern matching.match
in interface MatchableHandlerMapping
request
- the current requestpattern
- the pattern to matchnull
if noneprotected void registerHandler(String[] urlPaths, String beanName) throws BeansException, IllegalStateException
urlPaths
- the URLs that the bean should be mapped tobeanName
- the name of the handler beanBeansException
- if the handler couldn't be registeredIllegalStateException
- if there is a conflicting handler registeredprotected void registerHandler(String urlPath, Object handler) throws BeansException, IllegalStateException
urlPath
- the URL the bean should be mapped tohandler
- the handler instance or handler bean name String
(a bean name will automatically be resolved into the corresponding handler bean)BeansException
- if the handler couldn't be registeredIllegalStateException
- if there is a conflicting handler registeredpublic final Map<String,Object> getHandlerMap()
public final Map<PathPattern,Object> getPathPatternHandlerMap()
getHandlerMap()
but populated when parsed patterns
are enabled
; otherwise empty.protected boolean supportsTypeLevelMappings()
false
.