Sub topic 2
Choosing a versioning strategy
This chapter assumes you can build Spring MVC controllers and understand HTTP headers, query parameters and media types. It compares the four places a client can put a version, so you can pick one before you configure it.
What a strategy decides
A versioning strategy answers one question: where does the client state which version it wants? Spring Framework 7 supports four places. Each one maps to a version resolver, which later chapters configure.
- A request header, such as
X-API-Version: 2.0. - A path segment, such as
/v2/accounts/1. - A query parameter, such as
/accounts/1?version=2.0. - A media type parameter, such as
Accept: application/json;version=2.0.
The strategy affects URLs, caching, documentation and how clients are written. It is hard to change once clients depend on it.
Comparing the four options
| Strategy | Example | Strength | Weakness |
|---|---|---|---|
| Request header | `X-API-Version: 2.0` | URLs stay stable and clean | Hidden from browser address bars and plain links |
| Path segment | `/v2/accounts/1` | Visible, easy to test with a browser or `curl` | The URL of a resource changes between versions |
| Query parameter | `/accounts/1?version=2.0` | Easy to add and to try by hand | Easy to drop by accident, and it clutters cache keys |
| Media type parameter | `Accept: application/json;version=2.0` | Closest to HTTP content negotiation | Least familiar to clients, and tooling support varies |
Trade-offs that matter
URL stability. A resource such as account 1 is the same resource in every version. Header and media type strategies keep one URL for it. The path strategy gives it several URLs, which can break bookmarks and links stored in data.
Caching. Caches key on the URL. With a header or Accept value, the response must send a Vary header so a cache does not serve version 1 to a version 2 client. Path versions need no extra care.
Discoverability. Path and query versions can be pasted into a browser or a chat message. Header versions need a tool that can set headers.
Client effort. Every client must send the version. Header and media type versions are easy to add centrally, for example once in a RestClient builder. Path versions mean building a different URL for each call.
Gateways and routing. A path version is the easiest to route at a load balancer or API gateway. Routing on headers is possible but needs more configuration.
A practical way to choose
- Do public clients, such as browsers or third parties, call the API by hand? Prefer a path segment for visibility.
- Do you control the clients, or do they use generated SDKs? Prefer a header to keep URLs stable.
- Do you already use content negotiation heavily? A media type parameter fits that style.
- Do you need a quick option for internal tools? A query parameter works, but it is the easiest to omit by mistake.
Pick one strategy per API and apply it everywhere. Mixing strategies makes documentation and error handling harder. The examples in the rest of this tutorial use a request header, because it keeps the controller paths the same across versions.
Exercise
A mobile app is called by many third-party developers who test the API in browsers and share example URLs in forum posts. Which strategy fits best, and what do you give up?
Show answer
A path segment fits best, because the version is visible in every shared URL and works in a browser. You give up URL stability: the same account has a different URL in each version, so stored links must be updated when a version is retired.
Exercise
You choose a header strategy and a shared cache sits in front of the API. What must the response include so the cache does not mix up versions?
Show answer
A Vary header that names the version header, for example Vary: X-API-Version, so the cache stores a separate response per version value.