Sub topic 14
Evolving compatibly and retiring old versions
This chapter assumes you can configure ApiVersionConfigurer, map methods with the version attribute, and read the version errors covered in earlier chapters. It covers how to avoid creating new versions, and how to remove old ones safely.
Prefer compatible changes
Each version you add is one more thing to test, document and support. Most changes don't need one. A change is compatible when existing clients keep working without modification.
| Change | Compatible? |
|---|---|
| Add a new optional response field | Yes |
| Add a new endpoint | Yes |
| Accept a new optional request parameter | Yes |
| Remove or rename a response field | No |
| Change a field's type or meaning | No |
| Make an optional request field required | No |
| Add a new value to an enum clients switch on | Risky |
Clients that ignore unknown JSON fields survive additions. Clients that fail on them don't, so say in your API documentation that clients must ignore fields they don't know.
When a change is breaking, create a new version. In the AccountController from earlier chapters, version 2.0 split name into firstName and lastName. That is a rename, so it needs a new version.
Use baselines to avoid copying handlers
A baseline mapping such as 2.0+ matches 2.0 and every later supported version. If you add 2.1 with only compatible changes, you only add the version to the supported list:
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "2.0", "2.1")
.setDefaultVersion("1.0");
}The existing @GetMapping(path = "/{id}", version = "2.0+") method now serves 2.1 as well. You only write a new handler when 2.1 behaves differently. At that point, map the new method with 2.1+. Spring picks the most specific matching baseline, so 2.1 requests go to the new method and 2.0 requests stay on the old one.
Retire a version in stages
Removing a version abruptly breaks clients that you may not know about. Use a sequence instead.
- Deprecate. Mark the version as deprecated and tell clients which version replaces it. The earlier deprecation chapter showed how to signal this in responses.
- Announce a sunset date. Publish the date in your documentation, in a changelog, and in the response signals. Give clients a realistic period. Weeks may be enough for internal consumers, but public APIs usually need months.
- Watch usage. Log or count requests per version. Don't remove a version while real traffic still uses it, unless the date is a firm commitment.
- Remove. Delete the old handler methods and the version from the supported list.
What removal looks like
Take the version out of the configuration and delete its handlers:
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("2.0", "2.1")
.setDefaultVersion("2.0");
}The same change with Boot properties:
spring.mvc.apiversion.supported=2.0,2.1
spring.mvc.apiversion.default=2.0Two effects follow.
- A request that sends
X-API-Version: 1.0is now unsupported, so Spring rejects it with a 400 response. The client gets a clear error instead of silently receiving a different shape. - Requests with no version header used to resolve to 1.0. Changing the default to 2.0 moves them to the new response format. Those clients may break, so check who still relies on the default before you change it.
Keep tests in step
Before removal, add a test that sends the old version and expects the error. After removal, that test documents the behavior:
@Test
void retiredVersionIsRejected() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "1.0"))
.andExpect(status().isBadRequest());
}Delete tests for the removed handlers in the same commit. Leaving them behind causes failures that hide real problems.
Common mistakes
- Creating a version for every release instead of only for breaking changes.
- Removing a handler but leaving the version in the supported list. Spring then accepts the version, finds no handler and returns an error that is harder to interpret.
- Changing the default version without telling clients that send no version.
- Removing a version without a published date.
Exercise
Your API supports 1.0, 2.0 and 2.1, with the default set to 1.0. You want to retire 1.0 with no surprises for clients that send no version header. What do you change, and what do you check first?
Show answer
Check traffic first: count requests for 1.0 and requests that send no version header, since those resolve to 1.0 through the default. Announce a sunset date and wait for it. Then remove "1.0" from the supported versions, delete its handler methods, and set the default to a remaining version such as 2.0. Clients that send no header will receive the 2.0 format, so they must be told in advance. Requests that explicitly send 1.0 will be rejected with a 400 response.