Sub topic 3
Enabling versioning with ApiVersionConfigurer
Spring MVC does not version anything until you register a version source. This chapter shows the one configuration step that turns versioning on and what each part of it does.
Prerequisites
This chapter assumes you can write a @RestController with @GetMapping, define a @Configuration class, and read HTTP headers. It uses Spring Framework 7, which Spring Boot 4 builds on.
The configuration hook
Spring MVC exposes versioning through WebMvcConfigurer.configureApiVersioning. Spring passes you an ApiVersionConfigurer. You call methods on it to describe where the version comes from and which versions exist.
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ApiVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0")
.setDefaultVersion("1.0");
}
}Versioning is active only when you configure at least one version source. Without that, the version attribute on mappings has nothing to compare against.
What each call does
The three calls in the example map to three separate decisions.
| Call | Decision |
|---|---|
| `useRequestHeader("X-API-Version")` | Where the version is read from. |
| `addSupportedVersions(...)` | Which versions the application accepts. |
| `setDefaultVersion("1.0")` | Which version to use when a request has none. |
The first call picks a version resolver. A resolver extracts the raw version string from a request. The configurer offers one method per strategy: useRequestHeader, useQueryParam, usePathSegment and useMediaTypeParameter. You normally choose one. The next chapter covers each in detail.
The supported versions are the set Spring checks every incoming version against. A request for a version outside that set is rejected before any controller runs.
The default version fills in when the resolver finds nothing. Without it, a request that carries no version is treated as an error. Chapters later in this tutorial cover that behavior.
How a request flows through the configuration
If the resolver returns nothing, Spring substitutes the default version before the supported check. This ordering explains a common surprise: a default that is not in the supported set still fails the check.
Declaring support with care
addSupportedVersions can be called with several values, and it can be called more than once. Spring parses each string with the version parser, so "1.0" and "1" are treated as the same version. By default the parser handles dotted numeric versions.
Keep the supported list in step with your controllers. A version that appears in a version attribute but not in the supported set will never be reachable.
Common mistakes
- Configuring a resolver but forgetting
addSupportedVersions, so valid-looking versions are rejected. - Choosing a default that you plan to retire, which silently ties every unversioned client to it.
- Registering more than one resolver strategy and expecting Spring to combine them. Pick one source of truth.
- Putting the configuration in a class that is not a Spring bean, so
configureApiVersioningis never called.
Exercise
A client sends GET /accounts/1 with no X-API-Version header. The configurer calls addSupportedVersions("1.0", "2.0") and setDefaultVersion("1.5"). What happens, and how do you fix it?
Show answer
Spring substitutes the default, 1.5, and then checks it against the supported set. 1.5 is not in that set, so the request is rejected. Fix it by adding "1.5" with addSupportedVersions, or by changing the default to a version that is already supported, such as "1.0".