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 Summary
ConstructorsConstructorDescriptionDefaultApiVersionStrategy(List<ApiVersionResolver> versionResolvers, ApiVersionParser<?> versionParser, @Nullable Boolean versionRequired, @Nullable String defaultVersion, boolean detectSupportedVersions, @Nullable Predicate<Comparable<?>> supportedVersionPredicate, @Nullable ApiVersionDeprecationHandler deprecationHandler) Create an instance. -
Method Summary
Modifier 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 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ApiVersionStrategy
resolveParseAndValidateVersion
-
Constructor Details
-
DefaultApiVersionStrategy
public 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 usedversionParser- parser for raw version valuesversionRequired- whether a version is required leading toMissingApiVersionExceptionfor requests that don't have one; by default set to true unless there is a defaultVersiondefaultVersion- a default version to assign to requests that don't specify onedetectSupportedVersions- 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
-
getDefaultVersion
Description copied from interface:ApiVersionStrategyReturn a default version to use for requests that don't specify one.- Specified by:
getDefaultVersionin interfaceApiVersionStrategy
-
detectSupportedVersions
public 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...). -
addSupportedVersion
Add 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:
-
addMappedVersion
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.If you prefer to use explicitly configured, supported versions only, set the
detectSupportedVersionsflag tofalse.- Parameters:
versions- the versions to add- See Also:
-
resolveVersion
Description copied from interface:ApiVersionStrategyResolve the version value from a request, e.g. from a request header.- Specified by:
resolveVersionin interfaceApiVersionStrategy- Parameters:
request- the current request- Returns:
- the version, if present or
null - See Also:
-
parseVersion
Description copied from interface:ApiVersionStrategyParse the version of a request into an Object.- Specified by:
parseVersionin interfaceApiVersionStrategy- Parameters:
version- the value to parse- Returns:
- an Object that represents the version
- See Also:
-
validateVersion
public 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 interfaceApiVersionStrategy- Parameters:
requestVersion- the version to validaterequest- the request- Throws:
MissingApiVersionException- if the version is required, but not specifiedInvalidApiVersionException- if the version is not supported
-
handleDeprecations
public 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 interfaceApiVersionStrategy- Parameters:
version- the resolved and parsed request versionrequest- the current requestresponse- the current response- See Also:
-
toString
-