Sub topic 5
Declaring supported versions and how versions are parsed
This chapter assumes you can configure ApiVersionConfigurer and have already chosen a version resolver, such as the X-API-Version header used in earlier chapters. It covers two settings on that configurer: the set of supported versions, and the parser that turns version text into comparable objects.
Why Spring needs a supported set
A resolver only extracts a string such as "2.0" from the request. Spring then has to answer two questions about it. Is this a version the application knows about? And how does it compare with other versions, for example when a mapping says 2.0+?
The supported set answers the first question. The parser answers the second.
Declaring supported versions
Use addSupportedVersions to list the versions explicitly:
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");
}
}A request that carries a version outside this set is rejected before any controller runs. The default is a 400 Bad Request. The error chapter covers the details.
By default, Spring also detects versions from the version attribute of your mappings and adds them to the supported set. If you want the explicit list to be the only source of truth, turn detection off:
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "2.0")
.detectSupportedVersions(false);With detection off, a mapping that names a version missing from your list never matches a request. That is a useful safeguard, because it exposes forgotten registrations at the edge of the API instead of letting a new version appear by accident.
How versions are parsed
Spring does not compare version strings as text. Plain string comparison would sort "10" before "2". The configurer holds an ApiVersionParser, which converts each string into a comparable object. It applies the parser to three things:
- the version in the request,
- every supported version,
- every
versionattribute on a mapping.
The default parser is SemanticApiVersionParser. It reads major, minor and patch, and treats missing parts as zero. So "1", "1.0" and "1.0.0" are the same version. Text that does not fit this shape, such as "v1" or "latest", is invalid.
| Input | Default parser result |
|---|---|
| `2` | 2.0.0 |
| `1.1` | 1.1.0 |
| `1.1.3` | 1.1.3 |
| `v2` | invalid |
Using a custom parser
Suppose clients send v1 and v2, perhaps because the version sits in a path segment. Replace the parser with your own implementation of ApiVersionParser:
import org.springframework.web.accept.ApiVersionParser;
public class PrefixedIntegerVersionParser implements ApiVersionParser<Integer> {
@Override
public Integer parseVersion(String version) {
String digits = version.startsWith("v") ? version.substring(1) : version;
return Integer.parseInt(digits);
}
}configurer.usePathSegment(1)
.setVersionParser(new PrefixedIntegerVersionParser())
.addSupportedVersions("v1", "v2");Because the same parser reads mapping attributes, you now write version = "v2" on a controller method. The parsed value type only needs to be Comparable, so integers, semantic versions or dates all work.
Parsing and supported versions together
Common mistakes
- Listing
"2.0+"as a supported version. The+belongs only in mapping attributes. The supported set holds concrete versions. - Turning off detection and then adding a mapping for a new version without extending the list.
- Changing the parser after writing mappings, which can make existing
versionvalues unparseable.
Exercise
With the default parser and detection left on, a client sends X-API-Version: 2 and your supported list is "1.0", "2.0". Is the request accepted, and why? What happens if the client sends "v2"?
Show answer
"2" is accepted, because the default semantic parser fills missing parts with zero and reads it as 2.0.0, which equals the supported version "2.0". "v2" cannot be parsed by the default parser, so it is invalid and the request is rejected.