Sub topic 8
Fixed versions versus baseline versions
This chapter assumes you can write Spring MVC controllers with @GetMapping, and that you have enabled header-based versioning with ApiVersionConfigurer. It also assumes you know that supported versions are declared in that configuration. Here you will learn how the version attribute decides which handler serves a request, and why one notation is better for stable endpoints and the other for endpoints that rarely change.
Two notations in one attribute
The version attribute on @GetMapping, @PostMapping and @RequestMapping accepts a string. The string takes one of two forms:
- A fixed version, such as
"1.0". The handler matches only requests whose version equals1.0. - A baseline version, such as
"1.1+". The handler matches requests whose version is1.1or any later version.
The trailing + is the only difference. Spring parses the part before the + with the configured version parser, then compares the parsed values. It does not compare strings.
Comparing parsed values matters. With the default semantic parser, "2", "2.0" and "2.0.0" all parse to the same version. A request that sends 2 therefore matches a handler declared as "2.0".
A worked example
The controller below keeps three generations of one endpoint. Each generation changes the response shape.
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 AccountV11(long id, String name, String email) {}
record AccountV2(long id, String firstName, String lastName, String email) {}
@GetMapping(path = "/{id}", version = "1.0")
public AccountV1 getV1(@PathVariable long id) {
return new AccountV1(id, "Ada Lovelace");
}
@GetMapping(path = "/{id}", version = "1.1+")
public AccountV11 getV11(@PathVariable long id) {
return new AccountV11(id, "Ada Lovelace", "[email protected]");
}
@GetMapping(path = "/{id}", version = "2.0+")
public AccountV2 getV2(@PathVariable long id) {
return new AccountV2(id, "Ada", "Lovelace", "[email protected]");
}
}Pair it with this configuration:
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 ApiVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0")
.setDefaultVersion("1.0");
}
}Here is which handler answers each request version:
| Request version | `1.0` (fixed) | `1.1+` | `2.0+` | Handler that runs |
|---|---|---|---|---|
| 1.0 | matches | no | no | `getV1` |
| 1.1 | no | matches | no | `getV11` |
| 2.0 | no | matches | matches | `getV2` |
At 2.0, two handlers match because a baseline reaches upward. Spring does not report an ambiguity. It prefers the handler with the highest declared version, so 2.0+ beats 1.1+. The rule is that a request goes to the closest baseline at or below its version.
Why not write every version as fixed?
You can. Declaring "1.0", "1.1" and "2.0" as fixed versions is valid. It forces you to touch every endpoint each time you release a version. Suppose the API goes from 1.1 to 2.0, and only /accounts/{id} changes. Every other endpoint, such as /orders, would need its mapping edited to also accept 2.0, or clients on 2.0 would get no handler.
Baselines reverse this default. A mapping such as "1.0+" on /orders keeps serving 1.1, 2.0 and every later version until you add a new handler with a higher baseline. Only endpoints whose behavior actually changes need a new mapping.
The cost is that a baseline commits you to a promise: all future versions keep that behavior until you say otherwise. If you add 3.0 and a baseline handler is wrong for it, you must write a 3.0+ replacement. Otherwise clients on 3.0 silently get older behavior.
Choosing between them
Use a fixed version when:
- The handler serves exactly one release and should never be reached by later ones. This is typical for a deprecated endpoint you plan to remove.
- You want a hard failure when a new version is introduced and nobody has decided what the endpoint should do.
Use a baseline version when:
- The endpoint is stable across releases and you want new versions to inherit it.
- You are following an evolve-compatibly policy, where most versions differ by a small number of endpoints.
A practical pattern is to declare the oldest implementation with a baseline too, for example "1.0+", and add "2.0+" only where the contract breaks. Pin a fixed version only when you need to stop inheritance.
Mixing fixed and baseline mappings
Fixed and baseline mappings can share a path, as in the example, where 1.0 is fixed and the others are baselines. Two behaviors follow from how matching works.
- A fixed mapping stops inheritance. In the example,
getV1is fixed, so it never serves1.1. If you had written"1.0+", then1.1would have two matches,1.0+and1.1+, and the higher baseline1.1+would win. The result is identical, but the fixed form makes the intent explicit. - A gap is a gap. Suppose you declare only
"1.0"and"2.0+", and1.1is a supported version. A1.1request matches no handler. Spring does not fall back to1.0, because a fixed version means exactly that version. Expect a client error response rather than a reply from a neighboring version.
Avoid declaring a fixed and a baseline mapping at the same version on the same path, for example "2.0" and "2.0+". That pair gives ambiguous intent, and a reader cannot tell which one is meant to win. If you need it, add a test that sends 2.0 and asserts the response.
Baselines and the supported-versions list
A baseline only helps for versions Spring accepts. Before handler matching, the resolved request version is checked against the supported versions. A request for an unlisted version is rejected as invalid, with a 400 response by default, even if a baseline would have covered it.
Consider the configuration above, where 2.0 is the highest supported version. A request with X-API-Version: 2.1 is rejected, although 2.0+ covers it on paper. To serve 2.1, add it:
configurer.useRequestHeader("X-API-Version")
.addSupportedVersions("1.0", "1.1", "2.0", "2.1")
.setDefaultVersion("1.0");No controller change is needed. getV2 now answers 2.1 because 2.1 is greater than 2.0. This is the intended workflow for a compatible release: add the version to the supported list, and write a new handler only where behavior differs.
Because versions are parsed, ordering is numeric, not alphabetical. 1.10 is greater than 1.9, which string comparison would get wrong.
Common errors
- Forgetting the
+. You write"2.0"expecting later versions to be covered. Requests for2.1then find no handler. Check that every endpoint meant to be inherited has a baseline. - Writing the
+in the wrong place. The notation is"2.0+". Forms like"+2.0"are not baselines. - Supported list out of step with mappings. A new
3.0+handler is unreachable until3.0is added to the supported versions. - Assuming the default version covers everything.
setDefaultVersion("1.0")applies only when the request carries no version. It does not change how an explicit2.5is matched. - Reading a baseline as a minimum for the whole API.
"1.1+"applies to that one method. Other endpoints have their own mappings.
Testing the boundaries
Test the versions at the edges of each range, not only the versions in the middle. Using the MockMvc style from earlier chapters:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class BaselineMatchingTests {
@Autowired
MockMvc mockMvc;
@Test
void fixedVersionServesOnlyItself() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "1.0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").doesNotExist());
}
@Test
void baselineServesItsOwnVersion() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "1.1"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email").value("[email protected]"));
}
@Test
void highestBaselineWins() throws Exception {
mockMvc.perform(get("/accounts/1").header("X-API-Version", "2.0"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstName").value("Ada"));
}
}If you add 2.1 to the supported versions, add a fourth test that sends 2.1 and expects the 2.0+ shape. That test documents the baseline promise.
Summary of the rules
"1.1"matches only version 1.1."1.1+"matches 1.1 and later.- Versions are parsed and compared numerically, so
"2"and"2.0"are equal. - When several handlers match, the one with the highest declared version wins.
- Baselines cover only versions that are in the supported list.
- A gap between a fixed version and the next baseline leaves the versions in between unserved.
Exercise
An API supports versions 1.0, 1.1, 1.2 and 2.0. GET /orders has a single handler mapped with version = "1.1". A client sends X-API-Version: 1.2. What happens, and what one change to the mapping makes 1.2 and 2.0 work without adding handlers?
Show answer
The handler is a fixed version, so it matches only 1.1. A 1.2 request has no matching handler and gets a client error response. Changing the mapping to "1.1+" makes it a baseline, so it serves 1.1, 1.2 and 2.0. A request for 1.0 still finds no handler, because a baseline does not reach downward.
Exercise
You have handlers mapped "1.0+" and "2.0+" on the same path. A request arrives with version 2.3, and 2.3 is in the supported list. Which handler runs, and why? What happens if 2.3 is missing from the supported list?
Show answer
The "2.0+" handler runs. Both baselines match, and Spring picks the one with the highest declared version. If 2.3 is not in the supported list, the request is rejected as an invalid version before handler matching, with a 400 response by default.