Class DefaultApiVersionStrategy
java.lang.Object
org.springframework.web.accept.DefaultApiVersionStrategy
- All Implemented Interfaces:
- ApiVersionStrategy
Default implementation of 
ApiVersionStrategy that delegates to the
configured version resolvers, version parser, and deprecation handler.- Since:
- 7.0
- Author:
- Rossen Stoyanchev
- 
Constructor SummaryConstructorsConstructorDescriptionDefaultApiVersionStrategy(List<ApiVersionResolver> versionResolvers, ApiVersionParser<?> versionParser, @Nullable Boolean versionRequired, @Nullable String defaultVersion, boolean detectSupportedVersions, @Nullable Predicate<Comparable<?>> supportedVersionPredicate, @Nullable ApiVersionDeprecationHandler deprecationHandler) Create an instance.
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddMappedVersion(String... versions) Internal method to add to the list of actual version values that appear in request mappings, which allows supported versions to be discovered rather thanconfigured.voidaddSupportedVersion(String... versions) Add to the list of supported versions to check against inApiVersionStrategy.validateVersion(Comparable, HttpServletRequest)before raisingInvalidApiVersionExceptionfor unknown versions.booleanWhether the strategy is configured to detect supported versions.Return a default version to use for requests that don't specify one.voidhandleDeprecations(Comparable<?> version, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Check if the requested API version is deprecated, and if so handle it accordingly, e.g.Comparable<?> parseVersion(String version) Parse the version of a request into an Object.resolveVersion(jakarta.servlet.http.HttpServletRequest request) Resolve the version value from a request, e.g.toString()voidvalidateVersion(@Nullable Comparable<?> requestVersion, jakarta.servlet.http.HttpServletRequest request) Validate a request version, including required and supported version checks.Methods inherited from class Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ApiVersionStrategyresolveParseAndValidateVersion
- 
Constructor Details- 
DefaultApiVersionStrategypublic DefaultApiVersionStrategy(List<ApiVersionResolver> versionResolvers, ApiVersionParser<?> versionParser, @Nullable Boolean versionRequired, @Nullable String defaultVersion, boolean detectSupportedVersions, @Nullable Predicate<Comparable<?>> supportedVersionPredicate, @Nullable ApiVersionDeprecationHandler deprecationHandler) Create an instance.- Parameters:
- versionResolvers- one or more resolvers to try; the first non-null value returned by any resolver becomes the value used
- versionParser- parser for raw version values
- versionRequired- whether a version is required leading to- MissingApiVersionExceptionfor requests that don't have one; by default set to true unless there is a defaultVersion
- defaultVersion- a default version to assign to requests that don't specify one
- detectSupportedVersions- whether to use API versions that appear in mappings for supported version validation (true), or use only explicitly configured versions (false).
- deprecationHandler- handler to send hints and information about deprecated API versions to clients
 
 
- 
- 
Method Details- 
getDefaultVersionDescription copied from interface:ApiVersionStrategyReturn a default version to use for requests that don't specify one.- Specified by:
- getDefaultVersionin interface- ApiVersionStrategy
 
- 
detectSupportedVersionspublic boolean detectSupportedVersions()Whether the strategy is configured to detect supported versions. If this is set tofalsethenaddMappedVersion(String...)is ignored and the list of supported versions can be built explicitly through calls toaddSupportedVersion(String...).
- 
addSupportedVersionAdd to the list of supported versions to check against inApiVersionStrategy.validateVersion(Comparable, HttpServletRequest)before raisingInvalidApiVersionExceptionfor unknown versions.By default, actual version values that appear in request mappings are considered supported, and use of this method is optional. However, if you prefer to use only explicitly configured, supported versions, then set detectSupportedVersionsflag tofalse.- Parameters:
- versions- the supported versions to add
- See Also:
 
- 
addMappedVersionInternal method to add to the list of actual version values that appear in request mappings, which allows supported versions to be discovered rather thanconfigured.If you prefer to use explicitly configured, supported versions only, set the detectSupportedVersionsflag tofalse.- Parameters:
- versions- the versions to add
- See Also:
 
- 
resolveVersionDescription copied from interface:ApiVersionStrategyResolve the version value from a request, e.g. from a request header.- Specified by:
- resolveVersionin interface- ApiVersionStrategy
- Parameters:
- request- the current request
- Returns:
- the version, if present or null
- See Also:
 
- 
parseVersionDescription copied from interface:ApiVersionStrategyParse the version of a request into an Object.- Specified by:
- parseVersionin interface- ApiVersionStrategy
- Parameters:
- version- the value to parse
- Returns:
- an Object that represents the version
- See Also:
 
- 
validateVersionpublic void validateVersion(@Nullable Comparable<?> requestVersion, jakarta.servlet.http.HttpServletRequest request) throws MissingApiVersionException, InvalidApiVersionException Description copied from interface:ApiVersionStrategyValidate a request version, including required and supported version checks.- Specified by:
- validateVersionin interface- ApiVersionStrategy
- Parameters:
- requestVersion- the version to validate
- request- the request
- Throws:
- MissingApiVersionException- if the version is required, but not specified
- InvalidApiVersionException- if the version is not supported
 
- 
handleDeprecationspublic void handleDeprecations(Comparable<?> version, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Description copied from interface:ApiVersionStrategyCheck if the requested API version is deprecated, and if so handle it accordingly, e.g. by setting response headers to signal the deprecation, to specify relevant dates and provide links to further details.- Specified by:
- handleDeprecationsin interface- ApiVersionStrategy
- Parameters:
- version- the resolved and parsed request version
- request- the current request
- response- the current response
- See Also:
 
- 
toString
 
-