Sub topic 6
Mapping controller methods to versions
This chapter assumes you can write Spring MVC controllers with @RestController, @GetMapping and @PathVariable. It also assumes you know basic HTTP headers and have an application.properties file or a @Configuration class in your project. Versioning must already be switched on, as shown in the configuration chapter. Here we use a header named X-API-Version.
The version attribute
Spring Framework 7 adds a version attribute to @RequestMapping and to the shortcut annotations @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping. Two methods can share the same path and HTTP method. Spring chooses between them by comparing the version of the request with the version value on each mapping.
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");
}
}Without the version attribute, these two methods would both map to GET /accounts/{id}. Spring would then fail at startup with an ambiguous mapping error. With it, the mappings are distinct.
Fixed and baseline values
A value such as "1.0" is a fixed version. The method handles requests for exactly that version and no other.
A value with a trailing plus, such as "2.0+", is a baseline version. The method handles version 2.0 and every later supported version. Fixed and baseline values differ in what they do when a new version appears. A later chapter covers this in more detail.
| Mapping value | Request for 1.0 | Request for 1.1 | Request for 2.0 |
|---|---|---|---|
| `"1.0"` | matches | no match | no match |
| `"2.0+"` | no match | no match | matches |
If the supported versions are 1.0, 1.1 and 2.0, a request for 1.1 matches neither method above. Spring answers with a client error, not with the nearest version. If you want 1.1 to work, add a mapping for it or change the first mapping to a baseline such as "1.0+".
How Spring picks a handler
The matching happens in a few steps. Spring resolves the version from the request, for example from the header. It then parses it and checks that it is supported. Finally, it compares it with each candidate mapping.
A request that has no version header is not rejected at once. If a default version is configured, Spring uses it. That is why the example configuration falls back to 1.0.
Common errors
- Using the attribute without configuring versioning. If no version resolver is configured, a mapping that has a
versionattribute cannot be matched. Configure versioning first. - Using a version that is not in the supported list. A mapping for
3.0is never reached if3.0is not declared as supported, because the request is rejected earlier. - Expecting a fixed version to cover later versions.
"1.0"never matches1.1. Use"1.0+"when you want that. - Overlapping baselines.
"1.0+"and"2.0+"can both match a request for2.0. Spring prefers the more specific, higher baseline, but overlapping ranges make behavior harder to read. Keep ranges easy to reason about. - Mixing versioned and unversioned methods on one path. Methods without a
versionattribute match any version. Decide deliberately which methods are versioned.
Checking your mapping
You can confirm the mapping from outside with curl. The first call hits the version 1.0 method. The second hits the version 2.0 method.
curl -H "X-API-Version: 1.0" http://localhost:8080/accounts/1
curl -H "X-API-Version: 2.0" http://localhost:8080/accounts/1Exercise
The supported versions are 1.0, 1.1 and 2.0. A controller has one method mapped with version = "1.0" and another with version = "2.0+". What happens to a request for version 1.1, and how do you make it succeed?
Show answer
The request matches neither method, because "1.0" is fixed to exactly 1.0 and "2.0+" starts at 2.0. Spring returns a client error. To make it succeed, add a method mapped to "1.1", or change the first mapping to the baseline "1.0+" so that it covers 1.0 and 1.1.