Sub topic 1
Why and when to version an API
An API is a contract between a server and the clients that call it. This chapter covers when a change to that contract needs a new version and when it does not. Later chapters cover how Spring Boot 4 implements versioning.
Prerequisites
This chapter assumes you know the following:
- How to build REST controllers with Spring MVC (
@RestController,@GetMapping,@PathVariable). - Basic Spring Boot configuration (
application.properties,@Configurationbeans). - HTTP basics: headers, query parameters and status codes.
- Java records and basic Maven or Gradle usage.
What a version protects
Clients you do not control read your responses and send your requests in a fixed way. If you change the shape of a response, a client written last year can break. A version lets you publish the new shape next to the old one. Both stay available until the old one can be retired.
Spring Framework 7, which Spring Boot 4 builds on, has built-in API versioning for Spring MVC and WebFlux. Before that, teams hand-rolled it with separate paths or custom request conditions.
Breaking and compatible changes
Not every change needs a new version. The test is whether an existing, correctly written client keeps working.
| Change | Breaking? | Needs a new version? |
|---|---|---|
| Add an optional field to a response | No | No |
| Add a new endpoint | No | No |
| Add an optional request parameter | No | No |
| Rename or remove a response field | Yes | Yes |
| Split one field into two (`name` into `firstName` and `lastName`) | Yes | Yes |
| Change a field's type or meaning | Yes | Yes |
| Make an optional request parameter required | Yes | Yes |
| Change a status code for an existing case | Yes | Yes |
Adding fields is safe only if clients ignore unknown properties. Most JSON libraries do this by default, but check your consumers.
A running example
The chapters that follow use an accounts endpoint. Version 1.0 returns a single name field:
{ "id": 1, "name": "Ada Lovelace" }Version 2.0 splits the name:
{ "id": 1, "firstName": "Ada", "lastName": "Lovelace" }Splitting the field is a breaking change. Mobile apps that read name cannot be upgraded on your schedule, so both shapes must be served for a while.
When not to version
Versioning has a cost. Each live version is code you must test, document and secure. Consider alternatives first:
- Make the change additive. Keep the old field and add the new ones next to it.
- Control all the clients. If you deploy the server and every client together, a coordinated release can be simpler.
- Add a new endpoint. A different resource name sometimes fits better than a new version of an old one.
Version when you have independent clients that cannot upgrade at the same moment as the server.
How versions are chosen
A client can state the version it wants in several places: a request header, a path segment, a query parameter or a media type parameter. The next chapter compares these options. In Spring Boot 4 the choice is a configuration decision, and your controller methods then declare which versions they serve.
Exercise
An existing endpoint returns {"id": 7, "total": 12.50}. For each change, decide whether it needs a new version: (a) add a currency field, (b) rename total to amount, (c) change total from a number to a string.
Show answer
(a) No. It is an additive change, assuming clients ignore unknown fields. (b) Yes. Clients reading total will break. (c) Yes. The type change breaks clients that parse a number.
Exercise
Name two situations where you can avoid creating a new API version even though the response needs to change.
Show answer
Make the change additive by keeping the old fields and adding new ones, or deploy the server and all its clients together so you can release them in one step. Adding a new endpoint also works.