@RequestMapping supports API versioning by routing each version to a different handler, using a version in the URL path, a request header, a query parameter or a media type. Since Spring Framework 7 (Spring Boot 4), it also has a built-in version attribute with central configuration. On older versions you encode the version yourself in path, headers, params or produces.
Versioning lets you change an API without breaking existing clients. Whichever strategy you pick, apply it consistently and publish a deprecation policy for old versions.
Breaking changes, such as renaming a field, changing a type or removing an endpoint, break clients that cannot update immediately. A version identifier lets old and new contracts run side by side until clients migrate. Additive changes, like a new optional field, normally do not need a new version.
The version is part of the URL. It is the simplest and most visible option and is easy to cache, test and route at a gateway.
Note that @RequestMapping on a method requires a controller class. Putting one per version also makes it easy to delete a version later.
You can keep a stable URL and select the handler with the headers or params attribute.
Header versioning keeps URLs clean, but it is harder to try in a browser and caches must vary on the header (Vary: X-API-Version). Mixing several strategies on the same path can produce ambiguous mapping errors, so choose one.
This is content negotiation with a vendor media type. The client sends Accept: application/vnd.shop.v2+json and the produces attribute selects the handler.
It is the most REST-purist option but also the one clients find hardest to use and debug.
Spring Framework 7 (November 2025) added first-class API versioning. You configure how to read the version once, then declare version on mapping annotations:
The same configurer can read the version from a request header, a query parameter or a media type parameter instead of a path segment. A version such as 1.1 also matches a handler declared with 1.0+ (baseline version), which is useful for evolving APIs. If your project is on Spring Boot 3, use the manual strategies above and check the reference documentation before relying on this feature.
| Strategy | Pros | Cons |
|---|---|---|
Path (/v2/...) | Visible, cacheable, easy to route | URL changes per version |
| Header | Clean URLs | Hidden, needs Vary for caches |
| Query parameter | Easy to test | Easy to omit, pollutes caching |
| Media type | Standards-oriented | Complex for clients |
For public APIs, path versioning is the usual choice. For internal service-to-service calls, header or media type versioning can be a good fit.
There is no single best option. Path versioning is the most common and the easiest to operate, so pick it unless you have a reason not to.
No. Add a new version only for breaking changes. Additive, backward compatible changes can ship in the current version.
Announce a deprecation date, return Deprecation and Sunset headers where possible, and monitor traffic on the old version before removing it.
Yes. Keep the business logic in a service and map it to a version-specific DTO in each controller.
@RequestMapping can version an API by path, header, parameter or media type, and Spring Framework 7 adds a central version attribute. Choose one strategy, keep DTOs per version, share service logic and plan the retirement of old versions.