Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to Implement a RESTful API with Versioning in Spring

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 16, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • Choosing a versioning strategy
  • URI versioning with plain Spring MVC
  • Header, parameter and media type versioning
  • Native API versioning in Spring Framework 7
  • Keeping versions maintainable
  • Frequently Asked Questions
    • Which versioning style should I use?
    • Does Spring Boot 3 support the version attribute?
    • When should I create a new API version?
    • How do I document multiple versions?
  • Summary

Choosing a versioning strategy

Every strategy answers the same question: how does the client tell the server which contract it expects? The table compares the four common options.

StrategyExampleStrengthsWeaknesses
URI path/api/v2/users/1Obvious, easy to test in a browser, cache friendlyThe resource URL changes between versions
Query parameter/api/users/1?version=2Simple, URL path stays stableEasy to forget, pollutes cache keys
Request headerX-API-Version: 2Clean URLsHidden from casual testing, needs Vary for caches
Media typeAccept: application/vnd.example.v2+jsonFollows HTTP content negotiationHarder 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 with plain Spring MVC

URI versioning works on every Spring version and needs no extra configuration. Each version gets its own path prefix and, usually, its own DTO.

java

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.

Header, parameter and media type versioning

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.

java

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.

Native API versioning in Spring Framework 7

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.

java
java

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.

Keeping versions maintainable

Versioning multiplies code, so keep the cost low.

  1. Share the service layer. Controllers translate between versioned DTOs and one domain model; do not copy business logic per version.
  2. Put mapping code in one place, for example a mapper per version, so a change is easy to find.
  3. Announce deprecation early. Return a Deprecation or Sunset header on old versions and publish a retirement date.
  4. Cover each supported version with a test that calls it through MockMvc or WebTestClient.
  5. Retire versions. A version nobody removes becomes permanent maintenance work.

Frequently Asked Questions

Which versioning style should I use?

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.

Does Spring Boot 3 support the version attribute?

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.

When should I create a new API version?

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.

How do I document multiple versions?

Group endpoints per version in your OpenAPI tool, for example one GroupedOpenApi per version with springdoc, so each version has its own specification.

Summary

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.

Share