API Versioning

Spring WebFlux supports API versioning. This section provides an overview of the support and underlying strategies.spring-doc.cn

Please, see also related content in:spring-doc.cn

Client support for API versioning is available also in RestClient, WebClient, and HTTP Service clients, as well as for testing in WebTestClient.spring-doc.cn

ApiVersionStrategy

This is the central strategy for API versioning that holds all configured preferences related to versioning. It does the following:spring-doc.cn

ApiVersionStrategy helps to map requests to @RequestMapping controller methods, and is initialized by the WebFlux config. Typically, applications do not interact directly with it.spring-doc.cn

ApiVersionResolver

This strategy resolves the API version from a request. The WebFlux config provides built-in options to resolve from a header, query parameter, media type parameter, or from the URL path. You can also use a custom ApiVersionResolver.spring-doc.cn

The path resolver selects the version from a path segment specified by index, or raises InvalidApiVersionException, and therefore never results in null (no version) unless it is configured with a Predicate<RequestPath> to determine if a path is versioned.spring-doc.cn

ApiVersionParser

This strategy helps to parse raw version values into Comparable<?>, which helps to compare, sort, and select versions. By default, the built-in SemanticApiVersionParser parses a version into major, minor, and patWebFluxch integer values. Minor and patch values are set to 0 if not present.spring-doc.cn

Validation

If a request version is not supported, InvalidApiVersionException is raised resulting in a 400 response. By default, the list of supported versions is initialized from declared versions in annotated controller mappings, but you can turn that off through a flag in the WebFlux config, and use only the versions configured explicitly in the config.spring-doc.cn

By default, a version is required when API versioning is enabled, and MissingApiVersionException is raised resulting in a 400 response if not present. You can make it optional in which case the most recent version is used. You can also specify a default version to use.spring-doc.cn

ApiVersionDeprecationHandler

This strategy can be configured to send hints and information about deprecated versions to clients via response headers. The built-in StandardApiVersionDeprecationHandler can set the "Deprecation" "Sunset" headers and "Link" headers as defined in RFC 9745 and RFC 8594. You can also configure a custom handler for different headers.spring-doc.cn

Request Mapping

ApiVersionStrategy supports the mapping of requests to annotated controller methods. See API Versions for more details.spring-doc.cn