Sub topic 8
Fixed versions versus baseline versions
This chapter assumes you can write Spring MVC controllers and that you have enabled versioning with ApiVersionConfigurer, with supported versions 1.0, 1.1 and 2.0. It covers how the version attribute on a mapping decides which requests a method receives.
Two kinds of version value
The version attribute accepts two forms:
- A fixed version, such as
"1.0", matches requests for exactly that version. - A baseline version, such as
"1.0+", matches that version and every later supported version.
The + suffix appears only in mappings. A client always sends a plain version such as 1.1 and never 1.1+.
A gap with fixed versions
Suppose you map one method to "1.0" and another to "2.0+", as in the earlier example. Here is how requests are routed:
| Request version | Method that matches |
|---|---|
| 1.0 | `getV1` (fixed `1.0`) |
| 1.1 | None |
| 2.0 | `getV2` (baseline `2.0+`) |
Version 1.1 is declared as supported, so the version itself is valid. But no mapping covers it. Spring does not fall back to the nearest lower version, so the request fails. A fixed mapping would need a method for every supported version.
Closing the gap with a baseline
Baseline mappings avoid that problem. Each method says "this is the contract from this version onward".
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/accounts")
public class AccountController {
record AccountV1(long id, String name) {}
record AccountV2(long id, String firstName, String lastName) {}
@GetMapping(path = "/{id}", version = "1.0+")
public AccountV1 getV1(@PathVariable long id) {
return new AccountV1(id, "Ada Lovelace");
}
@GetMapping(path = "/{id}", version = "2.0+")
public AccountV2 getV2(@PathVariable long id) {
return new AccountV2(id, "Ada", "Lovelace");
}
}Both methods now match version 2.0, because 1.0+ includes it. Spring prefers the mapping with the higher version, so getV2 wins. The result:
| Request version | Method that handles it |
|---|---|
| 1.0 | `getV1` |
| 1.1 | `getV1` |
| 2.0 | `getV2` |
If you later add 2.1 to the supported versions, getV2 serves it without any code change.
Mixing fixed and baseline versions
You can combine both forms on the same path. A fixed mapping then overrides a baseline for one version only. For example, add @GetMapping(path = "/{id}", version = "1.1") next to "1.0+". Requests for 1.1 go to the fixed method. Version 1.0 still goes to the baseline method, and 2.0 still goes to getV2.
This is useful for a single endpoint that changed in a minor release, while the rest of the controller keeps its baseline.
Common errors
- Using only fixed versions and forgetting a supported version, which leaves a gap like
1.1above. - Writing
+in a client request or in thesupportedlist. Only mappings may use it. - Expecting a baseline to match versions that are not supported. A version outside the supported set is rejected before mapping is tried.
- Placing a baseline on a new method that returns a different shape. Every later version, including future ones, inherits that shape.
Exercise
The supported versions are 1.0, 1.1 and 2.0. A controller has methods mapped to "1.0+" and "1.1". Which method handles a request for version 1.1, and which handles 2.0?
Show answer
Version 1.1 matches both mappings, but the fixed "1.1" method is the one meant to override the baseline, so it handles 1.1. Version 2.0 matches only "1.0+" (the fixed "1.1" does not match it), so the "1.0+" method handles 2.0.