Sub topic 7
Default versions and required versions
This chapter assumes you can write Spring MVC controllers and @Configuration classes. It also assumes you know basic HTTP, and you have already set up a version resolver and a list of supported versions. Here you decide what happens when a request carries no version at all.
The problem: requests without a version
A resolver reads the version from the request, for example from the X-API-Version header. Some clients never send it. Older clients written before versioning existed are the usual case. Spring needs a rule for them, and you have two choices:
- Reject the request because a version is required.
- Pick a default version and handle the request with it.
Required versions
When no default version is set, a version is required. A request without one fails before it reaches any controller method.
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 StrictVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0");
}
}With this configuration, GET /accounts/1 with no header gets a 400 Bad Request response. The cause is a MissingApiVersionException. Spring's standard exception handling maps it to the 400 status.
Default versions
Call setDefaultVersion to give unversioned requests a version.
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0")
.setDefaultVersion("1.0");
}A request without the header is now handled as version 1.0. Setting a default also turns off the "required" rule, so the two settings work together. Spring decides that a version is required only if you have not set a default.
You can also set the rule explicitly with setVersionRequired(boolean). Treat that as an override for unusual cases. A default and setVersionRequired(true) contradict each other, so avoid combining them.
What a default does not do
A default applies only when the version is missing. If the client sends a version, Spring validates it against the supported versions, and the default plays no part.
curl -H "X-API-Version: 9.0" http://localhost:8080/accounts/1This request fails with 400 Bad Request (InvalidApiVersionException), even though a default exists. Spring does not silently fall back to the default for a wrong version, because that would hide client mistakes.
Choosing between them
| Situation | Setting |
|---|---|
| New API, all clients are under your control | Required, no default |
| Existing API that gains versioning later | Default set to the original version |
| Public API with unknown clients | Default set, pinned to the oldest stable version |
Common mistakes
- Setting the default to the newest version. When a later version is released, unversioned clients would silently switch and might break. Pin the default to a version you promise not to change.
- Choosing a default that is not supported. The default should be one of the versions you list as supported.
- Expecting the default to repair bad input. It does not. A wrong version is still an error.
Exercise
Your configuration supports versions 1.0 and 2.0 and sets the default to 1.0. What status does a client get when it sends X-API-Version: 3.0, and what happens when it sends no header?
Show answer
The request with 3.0 gets 400 Bad Request, because 3.0 is not supported and the default is used only when the version is missing. The request with no header is handled as version 1.0.