Sub topic 1
Why and when to version an API
An API version lets you change a service's contract without breaking the clients that depend on the old one. This chapter covers how to decide whether a change needs a new version at all. Later chapters cover the Spring Framework 7 and Spring Boot 4 machinery that serves several versions side by side.
Prerequisites
This chapter assumes you already know:
- How to write REST controllers with Spring MVC (
@RestController,@GetMapping,@PathVariable). - Basic Spring Boot configuration (
application.properties,@Configurationclasses). - HTTP basics: headers, query parameters and status codes.
- Java records and basic Maven or Gradle usage.
The chapter has no configuration steps. It explains the reasoning that the rest of the tutorial builds on.
The contract is more than the URL
A REST API is a contract between a server and its clients. The contract covers more than the paths and methods. It includes:
- The shape of request and response bodies: field names, types, nesting and which fields may be null.
- Status codes and error formats.
- Required and optional parameters and their accepted values.
- Headers the server reads or sets.
- Behaviour that clients rely on, such as sort order, default page size or idempotency.
You can edit the server's code in one deployment. You usually cannot update every client at the same moment. Mobile apps stay installed for years. Partner integrations follow their own release schedules. Even another team's service in your company deploys on its own timeline. Once a client exists, it has an implicit expectation of how your API behaves. Clients often depend on things you never documented. This is sometimes called Hyrum's law: with enough users, every observable behaviour becomes someone's dependency.
Versioning is the mechanism for changing the contract without forcing every client to change on your schedule. The old behaviour stays available under an old version. The new behaviour is available under a new one. Clients move when they are ready.
Breaking and compatible changes
Not every change needs a new version. The question is whether an existing, correctly written client keeps working after the change.
Take a response from GET /accounts/1:
{ "id": 1, "name": "Ada Lovelace" }Suppose you add a field:
{ "id": 1, "name": "Ada Lovelace", "createdAt": "2026-01-15T10:00:00Z" }A client that reads id and name still works. This is a compatible change. Now suppose you split name into firstName and lastName:
{ "id": 1, "firstName": "Ada", "lastName": "Lovelace" }Any client that reads name now gets nothing, or fails. This is a breaking change, and it is the case that justifies a version.
The following table classifies common changes. Treat it as a starting point, because your clients may be stricter than the table assumes.
| Change | Usually compatible? | Why |
|---|---|---|
| Add a new endpoint | Yes | No existing client calls it. |
| Add an optional response field | Yes, with tolerant clients | Clients that ignore unknown fields are unaffected. |
| Add an optional request parameter with a safe default | Yes | Old requests keep the old meaning. |
| Remove or rename a response field | No | Clients that read it break. |
| Change a field's type (number to string) | No | Deserialization fails or values are misread. |
| Make an optional request parameter required | No | Old requests now fail validation. |
| Change the meaning of a value without changing its shape | No | Clients silently compute wrong results. |
| Tighten validation | Often no | Requests that used to succeed are now rejected. |
| Change the error body format | No, if clients parse errors | Error handling code breaks. |
| Change status codes (200 to 204) | Often no | Clients that check status or read the body break. |
The last two rows are common gaps. Teams think of "the API" as the happy-path JSON and forget that error handling is also part of the contract.
Tolerant clients decide what is compatible
Adding a field is only safe if clients ignore fields they do not know. Jackson in Spring Boot does this by default for deserialization, because Spring Boot disables FAIL_ON_UNKNOWN_PROPERTIES. A client written with a strict parser, or a client that uses a generated schema with additionalProperties: false, will break on a new field.
Because of this, publish a rule alongside your API: "clients must ignore unknown fields and unknown enum values." That rule turns many changes into compatible ones. Without the rule you version more often and more painfully.
When you need a version
Introduce a new version when all of these are true:
- The change is breaking for at least one existing client.
- You cannot avoid the break by making the change additive.
- You cannot update all clients at the same time.
The diagram shows the decision.
When you do not need a version
Versioning has a cost. Every version you serve must be tested, secured, documented and monitored. Bugs fixed in one version often need to be fixed in the others. Skip versioning in these cases.
- Compatible changes. New fields and new endpoints belong in the current version.
- A private API with one client you deploy together. A backend-for-frontend that ships in the same release as its UI does not need a version. A coordinated deploy is cheaper.
- Bug fixes. If the API returned wrong data, correcting it is a fix. If clients depended on the bug, you have a judgement call, but do not create a version for every fix.
- Internal refactoring. Changing classes, database tables or libraries behind the controller does not change the contract.
Version the contract, not the release
A common mistake is to tie the API version to the application's release number. Your application may go from 3.4.1 to 3.5.0 every week, while the contract stays the same for a year. API versions should change only when the contract changes in a breaking way. Keep them in a short, simple form such as 1.0, 1.1 and 2.0, and keep them separate from the Maven or Gradle artifact version.
Another common mistake is to create a new version for every small change. Ten versions in two years means clients must track ten contracts and you must support them. Batch breaking changes into one major version, and use compatible additions in between.
What Spring Framework 7 provides
Before Spring Framework 7, teams built versioning by hand. They used path prefixes like /v1/accounts, custom request-mapping conditions, or produces media type tricks. Spring Framework 7, which Spring Boot 4 builds on, adds first-class API versioning for Spring MVC and WebFlux. The pieces are:
- A version resolver that reads the requested version from a header, path segment, query parameter or media type parameter.
- A list of supported versions and a parser that turns the raw text into a comparable version.
- A
versionattribute on mapping annotations such as@GetMapping, so methods that share a path can be separated by version. - Optional default and required version settings.
- Built-in error handling for missing and unsupported versions.
- Client support in
RestClientandWebClient, plus Spring Boot configuration properties.
The next chapters cover each piece. As a preview, the following controller shows the breaking change from earlier served as two versions. The version attribute is what separates the two methods.
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");
}
}This code needs versioning to be enabled before it will work, which a later chapter covers. Without that configuration, the version attribute has no resolver to read a version from. Do not worry about the 2.0+ syntax yet. The chapter on fixed and baseline versions explains it.
Common errors in deciding to version
- Treating "add a field" as breaking. This leads to needless versions. Fix it by documenting the tolerant-client rule.
- Treating "remove a field" as compatible because nobody seems to use it. You can rarely prove that. Check usage in logs or metrics first, and deprecate before removing.
- Versioning without a plan to retire the old version. An old version that never ends is permanent maintenance. Decide at the start how long you will support it.
- Forgetting behaviour and error contracts. Changes to validation, status codes and error bodies break clients as surely as renamed fields do.
- Copying the whole controller for each version. Most of the code does not change. Share services and map only the differing representations, as the records above do.
A short checklist
Before opening a branch for a new API version, answer these questions.
- Which clients does the change break, and who owns them?
- Can an additive design avoid the break?
- How will clients learn about the new version?
- How long will the old version live, and what happens when it ends?
- How will you measure which clients still use the old version?
If you cannot answer the last two, you are not ready to ship the new version.
Exercise
Your team plans three changes to GET /orders/{id}. (a) Add a trackingUrl field to the response. (b) Rename total to totalAmount. (c) Change status from the strings "OPEN"/"CLOSED" to numeric codes. The only consumers are a public mobile app and two partner services. Which changes need a new API version, and how would you handle the rest?
Show answer
Changes (b) and (c) are breaking. A client that reads total gets nothing after the rename, and a client that parses status as text fails on numbers. Mobile apps and partners cannot be updated at the same moment as the server, so both changes need a new version, ideally released together as one new major version. Change (a) is additive and compatible for clients that ignore unknown fields, so it ships in the current version with no new version. For (b) you could also avoid a version by returning both total and totalAmount for a while, then remove total in a later version.