Sub topic 4
Reading the version from a header, path, query parameter or media type
This chapter assumes you can write Spring MVC controllers and a @Configuration class. It also assumes you know what an HTTP header, a query parameter and a path segment are. The earlier chapters introduced ApiVersionConfigurer. Here you choose where Spring looks for the version.
What a version resolver does
A version resolver is the component that pulls a raw version string out of the incoming request. Spring then parses that string and matches it against the version attribute on your mappings. The resolver only locates the text. It does not decide whether the version is valid.
The built-in resolvers
You register a resolver by calling one use... method on the configurer. Each method corresponds to one versioning strategy.
| Strategy | Configurer method | Example request |
|---|---|---|
| Request header | `useRequestHeader("X-API-Version")` | `X-API-Version: 2.0` |
| Query parameter | `useQueryParam("version")` | `/accounts/1?version=2.0` |
| Path segment | `usePathSegment(0)` | `/2.0/accounts/1` |
| Media type parameter | `useMediaTypeParameter(MediaType.APPLICATION_JSON, "version")` | `Accept: application/json;version=2.0` |
Some details matter for each one:
- Header and query parameter: you pass the name. If the request lacks it, the resolver returns nothing, and the default or required rules apply.
- Path segment: the argument is a zero-based index into the path segments. The version is part of the URL, so your mapped paths must account for that segment.
- Media type parameter: the first argument is the media type whose parameter carries the version. Spring reads the parameter from the
Acceptheader.
Example: versioning through the Accept header
This configuration reads the version from a version parameter on application/json.
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ApiVersionConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MediaTypeVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useMediaTypeParameter(MediaType.APPLICATION_JSON, "version")
.addSupportedVersions("1.0", "2.0");
}
}The controller from the previous chapter works unchanged. Only the way the client states its version changes.
curl -H "Accept: application/json;version=2.0" http://localhost:8080/accounts/1Writing a custom resolver
If none of the built-in strategies fit, supply your own ApiVersionResolver. It is a functional interface that receives the request and returns the version string, or null when there is none. For example, you could read a cookie or a claim from a token. Register it with useVersionResolver(...). Keep the resolver small. Validation belongs to the supported-versions list, not to the resolver.
Choosing between them
- Headers and media type parameters keep URLs stable, but they are harder to try in a browser and easier to forget in a client.
- Query parameters and path segments are visible and easy to test by hand. Path segments also make caching by URL simple.
- Pick one strategy per API. Clients then have a single, predictable place to put the version.
Common errors
- Sending
X-API-Versionwhen the configurer reads a query parameter. The resolver finds nothing, so Spring applies the default version or rejects the request. - Using a media type parameter without setting an
Acceptheader. There is no parameter to read. - Forgetting that the path segment index is zero-based, so the resolver reads the wrong segment.
Exercise
Your clients send GET /accounts/1?v=2.0. Which configurer call do you use, and what happens to a request with no v parameter?
Show answer
Call useQueryParam("v"). With no v parameter the resolver returns nothing, so Spring uses the configured default version if there is one. Otherwise, if a version is required, it rejects the request as missing a version.