URL path versioning puts the API version in the request path, for example /api/v1/users and /api/v2/users. In Spring you implement it by mapping each version to its own controller (or its own @RequestMapping prefix) so that old clients keep working while new clients use the new contract. It is the most visible and cache-friendly versioning style, which is why most public APIs use it.
Spring Framework 7 and Spring Boot 4 also ship first-class API versioning support, so the manual approach below is what you use on Spring Boot 3.x or when you want full control.
The version is a literal segment of the path. Every version is a separate resource URL, so proxies, CDNs, logs and API gateways can route and cache each one independently.
| Request | Handled by |
|---|---|
GET /api/v1/users/42 | UserControllerV1 |
GET /api/v2/users/42 | UserControllerV2 |
Only bump the version for breaking changes: removing or renaming a field, changing a type, or changing behaviour clients depend on. Adding an optional field is not a breaking change and does not need v2.
The clearest layout is one controller per major version, each with its own response type. The example below uses Java records, which keep the DTOs short.
Calling GET /api/v1/users/42 returns the old shape and GET /api/v2/users/42 returns the new one:
Each class is public in its own file in a real project. Keep the business logic in a shared service and let the controllers only translate between the domain model and the version-specific DTO.
Duplicating the whole controller for every version gets expensive. Keep one service, and map to the DTO of each version at the edge.
Version 1 keeps its old contract even though the domain model moved on. This is the key benefit of versioned DTOs: you can refactor internals freely.
If versions differ only in a few endpoints, you can accept the version as a path variable and branch on it. This is quick, but it hides the contract in if statements, so prefer separate controllers once the versions diverge.
The regular expression v[12] makes unknown versions such as /api/v9/products return a 404 instead of falling into the handler.
Spring Framework 7 (Spring Boot 4) adds native support: you configure how the version is resolved (path segment, header, query parameter or media type) through ApiVersionConfigurer in a WebMvcConfigurer, and mapping annotations accept a version attribute. It also supports deprecation handling so you can signal that an old version will be retired. Check the Spring Framework reference documentation for the exact configuration for your version, and prefer it for new projects on Spring Boot 4. On Spring Boot 3.x, the controller-per-version approach above remains the standard.
Test every supported version so that a change to the shared service cannot silently break an old contract.
With springdoc-openapi you can publish one OpenAPI document per version using GroupedOpenApi with pathsToMatch("/api/v1/**"), so each version has its own documentation page.
Use only the major version, such as v1. Minor and patch changes must be backward compatible, so clients never need to choose them.
Announce a sunset date, add a Deprecation or Sunset response header, monitor traffic on the old path, and remove it once usage reaches zero or the date passes.
Path versioning is easier to test in a browser, cache and route. Header or media type versioning keeps URLs stable but is harder to debug, so choose path versioning unless you have a strong reason not to.
Yes. Only the endpoints with breaking changes need a new controller. Unchanged endpoints can be mapped under both prefixes if clients expect a complete version.
URL path versioning in Spring means putting the version in the route and giving each major version its own controller and DTOs. Share the business logic in services, change the version only for breaking changes, test every supported version, and plan deprecation from the start. On Spring Boot 4 you can use the built-in versioning support instead of doing the routing yourself.