Package org.springframework.web.servlet.mvc.method.annotation

# Class RequestMappingHandlerMapping

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.handler.AbstractHandlerMapping
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping<RequestMappingInfo>
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.ApplicationContextAware`, `org.springframework.context.EmbeddedValueResolverAware`, `org.springframework.core.Ordered`, `org.springframework.web.context.ServletContextAware`, `MatchableHandlerMapping`, `HandlerMapping`

public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMapping implements MatchableHandlerMapping, org.springframework.context.EmbeddedValueResolverAware
Creates `RequestMappingInfo` instances from type-level and method-level `@RequestMapping` and `@HttpExchange` annotations in `@Controller` classes.

Deprecation Note:

In 5.2.4, `useSuffixPatternMatch` and `useRegisteredSuffixPatternMatch` were deprecated in order to discourage use of path extensions for request mapping and for content negotiation (with similar deprecations in `ContentNegotiationManagerFactoryBean`). For further context, please read issue #24179.

Since:
  3.1
Author:
  Arjen Poutsma, Rossen Stoyanchev, Sam Brannen, Olga Maciaszek-Sharma

## Field Summary

### Fields inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping

`mappingsLogger`

### 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
`RequestMappingHandlerMapping()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
Detects handler methods at initialization.
`protected RequestMappingInfo`
`createRequestMappingInfo(org.springframework.web.bind.annotation.RequestMapping requestMapping, RequestCondition<?> customCondition)`
Create a `RequestMappingInfo` from the supplied `@RequestMapping` annotation, meta-annotation, or synthesized result of merging annotation attributes within an annotation hierarchy.
`protected RequestMappingInfo`
`createRequestMappingInfo(org.springframework.web.service.annotation.HttpExchange httpExchange, RequestCondition<?> customCondition)`
Create a `RequestMappingInfo` from the supplied `@HttpExchange` annotation, meta-annotation, or synthesized result of merging annotation attributes within an annotation hierarchy.
`RequestMappingInfo.BuilderConfiguration`
`getBuilderConfiguration()`
Obtain a `RequestMappingInfo.BuilderConfiguration` that reflects the internal configuration of this `HandlerMapping` and can be used to set `RequestMappingInfo.Builder.options(RequestMappingInfo.BuilderConfiguration)`.
`org.springframework.web.accept.ContentNegotiationManager`
`getContentNegotiationManager()`
Return the configured `ContentNegotiationManager`.
`protected RequestCondition<?>`
`getCustomMethodCondition(Method method)`
Provide a custom method-level request condition.
`protected RequestCondition<?>`
`getCustomTypeCondition(Class<?> handlerType)`
Provide a custom type-level request condition.
`List<String>`
`getFileExtensions()`
Deprecated.
as of 5.2.4.
`protected RequestMappingInfo`
`getMappingForMethod(Method method, Class<?> handlerType)`
Uses type-level and method-level `@RequestMapping` and `@HttpExchange` annotations to create the `RequestMappingInfo`.
`Map<String,Predicate<Class<?>>>`
`getPathPrefixes()`
The configured path prefixes as a read-only, possibly empty map.
`protected org.springframework.web.cors.CorsConfiguration`
`initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo)`
Extract and return the CORS configuration for the mapping.
`protected boolean`
`isHandler(Class<?> beanType)`
Whether the given type is a handler with handler methods.
`RequestMatchResult`
`match(HttpServletRequest request, String pattern)`
Determine whether the request matches the given pattern.
`protected void`
`registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping)`
Register a handler method and its unique mapping.
`void`
`registerMapping(RequestMappingInfo mapping, Object handler, Method method)`
Register the given mapping.
`protected String[]`
`resolveEmbeddedValuesInPatterns(String[] patterns)`
Resolve placeholder values in the given array of patterns.
`void`
`setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)`
Set the `ContentNegotiationManager` to use to determine requested media types.
`void`
`setEmbeddedValueResolver(org.springframework.util.StringValueResolver resolver)`
`void`
`setPathPrefixes(Map<String,Predicate<Class<?>>> prefixes)`
Configure path prefixes to apply to controller methods.
`void`
`setPatternParser(org.springframework.web.util.pattern.PathPatternParser patternParser)`
Set the `PathPatternParser` to parse `patterns` with for URL path matching.
`void`
`setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch)`
Deprecated.
as of 5.2.4.
`void`
`setUseSuffixPatternMatch(boolean useSuffixPatternMatch)`
Deprecated.
as of 5.2.4.
`void`
`setUseTrailingSlashMatch(boolean useTrailingSlashMatch)`
Deprecated.
as of 6.0, see `PathPatternParser.setMatchOptionalTrailingSeparator(boolean)`
`boolean`
`useRegisteredSuffixPatternMatch()`
Deprecated.
as of 5.2.4.
`boolean`
`useSuffixPatternMatch()`
Deprecated.
as of 5.2.4.
`boolean`
`useTrailingSlashMatch()`
Whether to match to URLs irrespective of the presence of a trailing slash.

### Methods inherited from class org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping

`getDirectPaths, getHandlerInternal, getMappingComparator, getMappingPathPatterns, getMatchingMapping, handleMatch, handleNoMatch`

### Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMethodMapping

`createHandlerMethod, detectHandlerMethods, getCandidateBeanNames, getCorsConfiguration, getHandlerMethods, getHandlerMethodsForMappingName, getNamingStrategy, handlerMethodsInitialized, hasCorsConfigurationSource, initHandlerMethods, lookupHandlerMethod, processCandidateBean, setDetectHandlerMethodsInAncestorContexts, setHandlerMethodMappingNamingStrategy, unregisterMapping`

### Methods inherited from class org.springframework.web.servlet.handler.AbstractHandlerMapping

`adaptInterceptor, detectMappedInterceptors, extendInterceptors, formatMappingName, getAdaptedInterceptors, getCorsConfigurationSource, getCorsHandlerExecutionChain, getCorsProcessor, getDefaultHandler, getHandler, getHandlerExecutionChain, getMappedInterceptors, getOrder, getPathMatcher, getPatternParser, getUrlPathHelper, initApplicationContext, initInterceptors, initLookupPath, setAlwaysUseFullPath, setBeanName, setCorsConfigurations, setCorsConfigurationSource, setCorsProcessor, setDefaultHandler, setInterceptors, setOrder, setPathMatcher, setRemoveSemicolonContent, setUrlDecode, setUrlPathHelper, usesPathPatterns`

### 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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface org.springframework.web.servlet.HandlerMapping

`getHandler, usesPathPatterns`

### Methods inherited from interface org.springframework.web.servlet.handler.MatchableHandlerMapping

`getPatternParser`

## Constructor Details

### RequestMappingHandlerMapping

public RequestMappingHandlerMapping()

## Method Details

### setPatternParser

public void setPatternParser(@Nullable org.springframework.web.util.pattern.PathPatternParser patternParser)
Description copied from class: `AbstractHandlerMapping`
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:

- `AbstractHandlerMapping.setAlwaysUseFullPath(boolean)` -- parsed patterns always use the full path and consider the servletPath only when a Servlet is mapped by path prefix.
- `AbstractHandlerMapping.setRemoveSemicolonContent(boolean)` -- parsed patterns always ignore semicolon content for path matching purposes, but path parameters remain available for use in controllers via `@MatrixVariable`.
- `AbstractHandlerMapping.setUrlDecode(boolean)` -- parsed patterns match one decoded path segment at a time and therefore don't need to decode the full path.
- `AbstractHandlerMapping.setUrlPathHelper(org.springframework.web.util.UrlPathHelper)` -- for parsed patterns, the request path is parsed once in `DispatcherServlet` or in `ServletRequestPathFilter` using `ServletRequestPathUtils` and cached in a request attribute.
- `AbstractHandlerMapping.setPathMatcher(org.springframework.util.PathMatcher)` -- a parsed patterns encapsulates the logic for path matching and does need a `PathMatcher`.

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.

Overrides:
  `setPatternParser` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
Parameters:
  `patternParser` - the parser to use

### setUseSuffixPatternMatch

@Deprecated public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch)
Deprecated.
as of 5.2.4. See class-level note on the deprecation of path extension config options.
Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled a method mapped to "/users" also matches to "/users.*".

By default value this is set to `false`.

Also see `setUseRegisteredSuffixPatternMatch(boolean)` for more fine-grained control over specific suffixes to allow.

Note: This property is ignored when `setPatternParser(PathPatternParser)` is configured.

### setUseRegisteredSuffixPatternMatch

@Deprecated public void setUseRegisteredSuffixPatternMatch(boolean useRegisteredSuffixPatternMatch)
Deprecated.
as of 5.2.4. See class level note on the deprecation of path extension config options.
Whether suffix pattern matching should work only against path extensions explicitly registered with the `ContentNegotiationManager`. This is generally recommended to reduce ambiguity and to avoid issues such as when a "." appears in the path for other reasons.

By default this is set to "false".

Note: This property is ignored when `setPatternParser(PathPatternParser)` is configured.

### setUseTrailingSlashMatch

@Deprecated(since="6.0") public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch)
Deprecated.
as of 6.0, see `PathPatternParser.setMatchOptionalTrailingSeparator(boolean)`
Whether to match to URLs irrespective of the presence of a trailing slash. If enabled a method mapped to "/users" also matches to "/users/".

The default was changed in 6.0 from `true` to `false` in order to support the deprecation of the property.

### setPathPrefixes

public void setPathPrefixes(Map<String,Predicate<Class<?>>> prefixes)
Configure path prefixes to apply to controller methods.

Prefixes are used to enrich the mappings of every `@RequestMapping` method and `@HttpExchange` method whose controller type is matched by a corresponding `Predicate` in the map. The prefix for the first matching predicate is used, assuming the input map has predictable order.

Consider using `HandlerTypePredicate` to group controllers.

Parameters:
  `prefixes` - a map with path prefixes as key
Since:
  5.1

### getPathPrefixes

public Map<String,Predicate<Class<?>>> getPathPrefixes()
The configured path prefixes as a read-only, possibly empty map.

Since:
  5.1

### setContentNegotiationManager

public void setContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager contentNegotiationManager)
Set the `ContentNegotiationManager` to use to determine requested media types. If not set, the default constructor is used.

### getContentNegotiationManager

public org.springframework.web.accept.ContentNegotiationManager getContentNegotiationManager()
Return the configured `ContentNegotiationManager`.

### setEmbeddedValueResolver

public void setEmbeddedValueResolver(org.springframework.util.StringValueResolver resolver)

Specified by:
  `setEmbeddedValueResolver` in interface `org.springframework.context.EmbeddedValueResolverAware`

### afterPropertiesSet

public void afterPropertiesSet()
Description copied from class: `AbstractHandlerMethodMapping`
Detects handler methods at initialization.

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
Overrides:
  `afterPropertiesSet` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
See Also:
  - `AbstractHandlerMethodMapping.initHandlerMethods()`

### useSuffixPatternMatch

@Deprecated public boolean useSuffixPatternMatch()
Deprecated.
as of 5.2.4. See deprecation notice on `setUseSuffixPatternMatch(boolean)`.
Whether to use registered suffixes for pattern matching.

### useRegisteredSuffixPatternMatch

@Deprecated public boolean useRegisteredSuffixPatternMatch()
Deprecated.
as of 5.2.4. See deprecation notice on `setUseRegisteredSuffixPatternMatch(boolean)`.
Whether to use registered suffixes for pattern matching.

### useTrailingSlashMatch

public boolean useTrailingSlashMatch()
Whether to match to URLs irrespective of the presence of a trailing slash.

### getFileExtensions

@Nullable @Deprecated public List<String> getFileExtensions()
Deprecated.
as of 5.2.4. See class-level note on the deprecation of path extension config options.
Return the file extensions to use for suffix pattern matching.

### getBuilderConfiguration

public RequestMappingInfo.BuilderConfiguration getBuilderConfiguration()
Obtain a `RequestMappingInfo.BuilderConfiguration` that reflects the internal configuration of this `HandlerMapping` and can be used to set `RequestMappingInfo.Builder.options(RequestMappingInfo.BuilderConfiguration)`.

This is useful for programmatic registration of request mappings via `registerHandlerMethod(Object, Method, RequestMappingInfo)`.

Returns:
  the builder configuration that reflects the internal state
Since:
  5.3.14

### isHandler

protected boolean isHandler(Class<?> beanType)
Whether the given type is a handler with handler methods.

Expects a handler to have a type-level @`Controller` annotation.

Specified by:
  `isHandler` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
Parameters:
  `beanType` - the type of the bean being checked
Returns:
  "true" if this a handler type, "false" otherwise.

### getMappingForMethod

@Nullable protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType)
Uses type-level and method-level `@RequestMapping` and `@HttpExchange` annotations to create the `RequestMappingInfo`.

Specified by:
  `getMappingForMethod` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
Parameters:
  `method` - the method to provide a mapping for
  `handlerType` - the handler type, possibly a subtype of the method's declaring class
Returns:
  the created `RequestMappingInfo`, or `null` if the method does not have a `@RequestMapping` or `@HttpExchange` annotation
See Also:
  - `getCustomMethodCondition(Method)`
  - `getCustomTypeCondition(Class)`

### getCustomTypeCondition

@Nullable protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType)
Provide a custom type-level request condition. The custom `RequestCondition` can be of any type so long as the same condition type is returned from all calls to this method in order to ensure custom request conditions can be combined and compared.

Consider extending `AbstractRequestCondition` for custom condition types and using `CompositeRequestCondition` to provide multiple custom conditions.

Parameters:
  `handlerType` - the handler type for which to create the condition
Returns:
  the condition, or `null`

### getCustomMethodCondition

@Nullable protected RequestCondition<?> getCustomMethodCondition(Method method)
Provide a custom method-level request condition. The custom `RequestCondition` can be of any type so long as the same condition type is returned from all calls to this method in order to ensure custom request conditions can be combined and compared.

Consider extending `AbstractRequestCondition` for custom condition types and using `CompositeRequestCondition` to provide multiple custom conditions.

Parameters:
  `method` - the handler method for which to create the condition
Returns:
  the condition, or `null`

### createRequestMappingInfo

protected RequestMappingInfo createRequestMappingInfo(org.springframework.web.bind.annotation.RequestMapping requestMapping, @Nullable RequestCondition<?> customCondition)
Create a `RequestMappingInfo` from the supplied `@RequestMapping` annotation, meta-annotation, or synthesized result of merging annotation attributes within an annotation hierarchy.

### createRequestMappingInfo

protected RequestMappingInfo createRequestMappingInfo(org.springframework.web.service.annotation.HttpExchange httpExchange, @Nullable RequestCondition<?> customCondition)
Create a `RequestMappingInfo` from the supplied `@HttpExchange` annotation, meta-annotation, or synthesized result of merging annotation attributes within an annotation hierarchy.

Since:
  6.1

### resolveEmbeddedValuesInPatterns

protected String[] resolveEmbeddedValuesInPatterns(String[] patterns)
Resolve placeholder values in the given array of patterns.

Returns:
  a new array with updated patterns

### registerMapping

public void registerMapping(RequestMappingInfo mapping, Object handler, Method method)
Description copied from class: `AbstractHandlerMethodMapping`
Register the given mapping.

This method may be invoked at runtime after initialization has completed.

Overrides:
  `registerMapping` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
Parameters:
  `mapping` - the mapping for the handler method
  `handler` - the handler
  `method` - the method

### registerHandlerMethod

protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping)
Register a handler method and its unique mapping. Invoked at startup for each detected handler method.

Note: To create the `RequestMappingInfo`, please use `getBuilderConfiguration()` and set the options on `RequestMappingInfo.Builder.options(RequestMappingInfo.BuilderConfiguration)` to match how this `HandlerMapping` is configured. This is important for example to ensure use of `PathPattern` or `PathMatcher` based matching.

Overrides:
  `registerHandlerMethod` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`
Parameters:
  `handler` - the bean name of the handler or the handler instance
  `method` - the method to register
  `mapping` - the mapping conditions associated with the handler method

### match

@Nullable public RequestMatchResult match(HttpServletRequest request, String pattern)
Description copied from interface: `MatchableHandlerMapping`
Determine whether the request matches the given pattern. Use this method when `MatchableHandlerMapping.getPatternParser()` returns `null` which means that the `HandlerMapping` is using String pattern matching.

Specified by:
  `match` in interface `MatchableHandlerMapping`
Parameters:
  `request` - the current request
  `pattern` - the pattern to match
Returns:
  the result from request matching, or `null` if none

### initCorsConfiguration

@Nullable protected org.springframework.web.cors.CorsConfiguration initCorsConfiguration(Object handler, Method method, RequestMappingInfo mappingInfo)
Description copied from class: `AbstractHandlerMethodMapping`
Extract and return the CORS configuration for the mapping.

Overrides:
  `initCorsConfiguration` in class `AbstractHandlerMethodMapping<RequestMappingInfo>`