API Versioning Is Finally Built Into Spring
For fifteen years, every Spring team versioned its REST API with a homemade mix of URL prefixes, custom headers and hope. Spring Framework 7 and Spring Boot 4 make versioning a first-class part of request mapping: declare where the version lives, tag your handlers, and let the framework route, validate and deprecate. Here is how it works on the server and the client, and the mistakes it quietly stops you from making.
- #spring-boot
- #java
- #api-design
- #backend
- #rest
Every REST API eventually gets a second version. Nobody plans for it on day one, which is exactly why the first version ships with no plan at all.
Then a field has to change shape, a mobile app from two years ago is still out there calling the old one, and someone opens a ticket titled "add v2". What happens next depends on the team. One copies the controller into a v2 package. Another adds if (header.equals("2")) inside the method. A third invents a custom annotation, a request condition and a filter, and leaves the company eighteen months later with the only copy of how it works in their head.
All three approaches work. None of them are consistent across services, and none of them tell a client "this version is going away on the 1st of March".
Spring Framework 7 (shipped with Spring Boot 4) finally makes versioning part of the framework. You tell Spring where the version lives in the request, you tag handlers with the versions they serve, and Spring does the routing. It's one of those features that makes you wonder why it took this long, like the second USB plug orientation.
Step one: decide where the version lives
There are four common places to carry an API version, and Spring supports all of them:
- A request header, e.g.
API-Version: 1.2 - A query parameter, e.g.
/orders?version=1.2 - A path segment, e.g.
/v1.2/orders - A media type parameter, e.g.
Accept: application/json;version=1.2
Which one you pick is mostly a matter of taste and of what your gateway and caches can see. Headers keep URLs clean. Path segments are easy to see in logs and easy to route at a load balancer. Query parameters are easy to test from a browser. Media types make REST purists happy and everyone else slightly confused.
The important part is that you pick one, for every service, and write it down. In Spring Boot 4 that's a property:
spring.mvc.apiversion.use.header=API-Version
spring.mvc.apiversion.default=1.0Or, if you prefer code, the same thing in a WebMvcConfigurer:
@Configuration
class ApiVersionConfig implements WebMvcConfigurer {
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
configurer
.useRequestHeader("API-Version")
.setDefaultVersion("1.0")
.addSupportedVersions("1.0", "1.1", "2.0");
}
}The other strategies are useQueryParam(...), usePathSegment(index) and useMediaTypeParameter(...). WebFlux has the same model through WebFluxConfigurer.
Step two: tag the handlers
Mapping annotations now take a version attribute:
@RestController
@RequestMapping("/orders")
class OrderController {
@GetMapping(path = "/{id}", version = "1.0")
OrderV1 getV1(@PathVariable String id) { ... }
@GetMapping(path = "/{id}", version = "2.0")
OrderV2 getV2(@PathVariable String id) { ... }
}Same path, two handlers, and Spring picks one based on the version in the request. No if statements, no copied controller package.
The part that saves the most code is the baseline version. Most endpoints don't change between versions, so you don't want to tag every handler with every version number. Add a +:
@GetMapping(path = "/{id}/status", version = "1.0+")
OrderStatus status(@PathVariable String id) { ... }1.0+ means "1.0 and anything after it, until a more specific mapping takes over". When you ship 2.0 and only three endpoints changed, those three get new handlers and everything else keeps answering on its baseline. It's the API equivalent of only repainting the walls that got scuffed.
What the framework now does for you
Before this, versioning code mostly did routing and left the edge cases to luck. Built-in support handles them on purpose.
Unknown versions get rejected. If a client sends API-Version: 7.3 and you never declared it, Spring returns a 400 instead of quietly routing it somewhere. With the homemade approach, an unknown version usually fell through to whatever the else branch did, which is how clients end up depending on behaviour nobody designed.
Missing versions get a decision. You choose: use a default version, or require one with setVersionRequired(true). Defaults are friendlier to existing clients. Required versions are stricter and better for new internal APIs where you control every caller.
Versions are parsed, not string-compared. 1.10 is newer than 1.9. You'd be surprised how many homemade solutions disagree.
Deprecation, announced properly
This is the feature I'd bring up first if someone asked whether it's worth migrating.
Retiring an old version is mostly a communication problem. You need to tell clients the version is deprecated, when it will stop working, and where to read about it. Two standards already cover that: the Deprecation header (RFC 9745) and the Sunset header (RFC 8594), plus a Link header pointing at docs.
Spring can now send them for you. Configure a deprecation handler for the versions on their way out, and every response for those versions carries the headers automatically:
@Override
public void configureApiVersioning(ApiVersionConfigurer configurer) {
StandardApiVersionDeprecationHandler handler =
new StandardApiVersionDeprecationHandler();
handler.configureVersion("1.0")
.setDeprecationDate(ZonedDateTime.parse("2026-06-01T00:00:00Z"))
.setSunsetDate(ZonedDateTime.parse("2026-12-01T00:00:00Z"))
.setDeprecationLink(URI.create("https://api.example.com/docs/migrate-to-v2"));
configurer
.useRequestHeader("API-Version")
.setDeprecationHandler(handler);
}Now the sunset date isn't a line in a Confluence page that one client team read. It's in every response, where client logging, gateways and contract tests can actually see it. (Whether anyone reads their own logs is a separate problem, and not one Spring can fix.)
The client side gets it too
Versioning only on the server means every client builds its own way of sending the header. Spring's RestClient and WebClient now take an API version inserter, so the client sets the version the same way the server expects it:
RestClient client = RestClient.builder()
.baseUrl("https://orders.internal")
.apiVersionInserter(ApiVersionInserter.useHeader("API-Version"))
.build();
OrderV2 order = client.get()
.uri("/orders/{id}", id)
.apiVersion("2.0")
.retrieve()
.body(OrderV2.class);You can also set a default version on the builder, so every call from one client uses the same one. For a codebase with many services calling each other, this is the bit that keeps versioning consistent: both ends use the same strategy, declared once.
Mistakes it stops you from making
A short list, based on what homemade versioning tends to get wrong:
- Silent fallthrough. Unknown versions used to land in a default branch. Now they're a 400.
- Copy-paste controllers. A
v2package that duplicates forty endpoints to change three. Baseline versions remove the reason to do it. - String comparison.
"1.10" < "1.9"is true for strings. Parsed versions get it right. - Deprecation by email. Sunset dates now travel with the response.
- Inconsistent strategies. One service uses a header, another a path prefix. One configuration per service, ideally shared in a common library, fixes this.
That last one is worth a moment. If you run more than a handful of services, put the versioning configuration in your shared starter or core library. Then "where does the version go" is answered once for the whole platform, instead of in each team's first design review.
When not to bother
Versioning is a tool for breaking changes. If you can make a change without breaking anyone, do that instead:
- Adding a field to a response isn't breaking for well-behaved clients. Ship it in the current version.
- Adding an optional request field isn't breaking either.
- Renaming or removing a field, or changing its type, is breaking. That's what versions are for.
A healthy API has few versions, each with a clear end date. An API with eleven live versions doesn't have a versioning strategy. It has a museum.
Migrating from what you have now
If you already version with path prefixes like /v1/orders, the least disruptive move is the path-segment strategy, so URLs don't change for clients:
configurer.usePathSegment(0);Then move version logic out of method bodies and into version attributes, one controller at a time. Each step is small, testable, and invisible to clients, which is the only kind of migration anyone enjoys.
If your versions live in a custom header, useRequestHeader with the same name gets you there without touching clients at all.
The takeaway
Versioning a REST API was never a hard problem. It was an inconsistent one: every team built its own mix of URL prefixes, custom headers and if statements, and no two of them treated unknown versions, missing versions or retirement the same way. Spring Framework 7 and Spring Boot 4 move all of it into request mapping. Pick one place for the version, configure it once (in a shared starter, if you run more than a handful of services), and tag handlers with version. Use 1.0+ baselines so a new version only costs you handlers for the endpoints that actually changed.
Then let the framework do the parts homemade code used to skip. An unknown version becomes a 400 instead of a trip through somebody's else branch, a missing version gets a decision you made on purpose, and 1.10 is finally newer than 1.9. Retirement dates go out as Deprecation and Sunset headers on every response, where clients and gateways can see them, and apiVersionInserter makes the client send the version exactly the way the server expects it.
Versioning was never hard to build. It was hard to build the same way twice. Now you don't have to build it at all.
/share

Kishore K Sharma
Module Lead | Senior Full Stack Engineer | Java · Spring Boot · Distributed Systems · AWS | Building Scalable Cloud-Native Platforms
Available for contract work and remote full-time roles. See what I take on.
/seen in production