To version a RESTful API in Spring, choose one place for the version (URI path, request header, query parameter or media type) and route each request to the matching controller method. Spring Framework 7 and Spring Boot 4 added first-class API versioning, so you can declare version = "2" on a mapping and configure how the version is read in one place. On Spring Framework 6 and Spring Boot 3 you implement the same strategies yourself with @RequestMapping conditions.
The right choice depends on who calls your API. Public APIs usually favour a version in the URL because it is visible and easy to cache, while internal APIs often use headers to keep URLs stable.
Every strategy answers the same question: how does the client tell the server which contract it expects? The table compares the four common options.
| Strategy | Example | Strengths | Weaknesses |
|---|---|---|---|
| URI path | /api/v2/users/1 | Obvious, easy to test in a browser, cache friendly | The resource URL changes between versions |
| Query parameter | /api/users/1?version=2 | Simple, URL path stays stable | Easy to forget, pollutes cache keys |
| Request header | X-API-Version: 2 | Clean URLs | Hidden from casual testing, needs Vary for caches |
| Media type | Accept: application/vnd.example.v2+json | Follows HTTP content negotiation | Harder for clients and documentation |
Whatever you choose, version only when you make a breaking change, such as removing a field, renaming a field or changing its meaning. Adding optional fields is backward compatible and does not need a new version.
URI versioning works on every Spring version and needs no extra configuration. Each version gets its own path prefix and, usually, its own DTO.
A request to /api/v1/users/1 returns {"id":1,"name":"Ada Lovelace"}, while /api/v2/users/1 returns the split name fields. Records keep each version's response shape explicit and immutable.
Without native support, you narrow a mapping with the headers, params or produces attributes. Spring only dispatches the request to the method whose condition matches.
If no condition matches, Spring answers with 400 Bad Request for a missing parameter or 404/406 for the other cases. Decide on a default version for clients that send nothing, and document it. When the version lives in a header, add Vary: X-API-Version to responses so shared caches do not mix versions.
Spring Framework 7 (the base of Spring Boot 4) adds a version attribute to @RequestMapping and its shortcuts, plus a central configuration hook. You declare how the version is resolved once, and the mappings only name the version they serve.
The configurer can also read the version from a path segment, a query parameter or a media type parameter instead of a header. Requests that name an unsupported version are rejected for you, and a version mapping such as "1.2+" can mean "this version and later". Check the Spring Framework reference for the exact configurer methods in your release, since this API is new.
Versioning multiplies code, so keep the cost low.
Deprecation or Sunset header on old versions and publish a retirement date.MockMvc or WebTestClient.URI versioning is the safest default for public APIs because clients, proxies and documentation tools understand it immediately. Choose headers or media types when you need stable URLs or strict content negotiation.
No. The version attribute and ApiVersionConfigurer arrive with Spring Framework 7 and Spring Boot 4. On Boot 3 use path prefixes, params, headers or produces.
Only for breaking changes: removed or renamed fields, changed types or changed behaviour. Adding optional fields or new endpoints does not need a new version.
Group endpoints per version in your OpenAPI tool, for example one GroupedOpenApi per version with springdoc, so each version has its own specification.
Pick one versioning location and use it consistently. URI paths work everywhere and are easiest for clients; headers, parameters and media types keep URLs stable but need more care with caching and documentation. Spring Framework 7 adds native version support with central configuration, and older releases achieve the same with request mapping conditions. Share business logic across versions, deprecate with clear dates and remove old versions on schedule.