Implementing API Versioning

1. Implementing URL Path Versioning

AspectDetail
Format/v{n}/resource
ProsVisible, cacheable, easy to route
ConsResource has multiple URIs

Example: Versioned controller

@RestController
@RequestMapping("/v1/orders")
public class OrderControllerV1 { ... }

2. Implementing Header-Based Versioning

HeaderExample
CustomAPI-Version: 2
Spring@RequestMapping(headers = "API-Version=2")
ProClean URLs
ConLess discoverable, harder to test in browser

3. Implementing Query Parameter Versioning

AspectDetail
Format?version=2
ProEasy fallback to default
ConCaches must include in key

4. Implementing Content Negotiation Versioning

HeaderExample
Acceptapplication/vnd.acme.v2+json
Spring@RequestMapping(produces = "application/vnd.acme.v2+json")
ProRESTful, leverages existing header
ConVerbose, harder for casual clients

5. Implementing Versioned Controllers

StrategyDetail
One per versionIndependent evolution
Shared serviceLogic centralized; controllers map
Adapter patternv1 controller → v2 service via adapter

6. Implementing Versioned DTOs

Example: Version packages

com.acme.api.v1.OrderResponse
com.acme.api.v2.OrderResponse  (added 'channel' field)
com.acme.api.mapper.OrderMapperV1, OrderMapperV2

7. Implementing Backward Compatibility Logic

ChangeCompatible?
Add optional field (response)Yes
Add required field (request)No
Remove fieldNo
Rename fieldNo (use alias)
Change typeNo
Loosen validationYes
Tighten validationNo

8. Implementing Deprecation Warnings

Example: Deprecation headers

HTTP/1.1 200 OK
Deprecation: true
Sunset: Sat, 31 Dec 2026 23:59:59 GMT
Link: </v2/orders>; rel="successor-version"

9. Implementing Version Migration Logic

ApproachDetail
Translation layerv1 request → v2 internal
Default valuesFill new required fields
Field rewriteOld name → new name

10. Managing Parallel Version Support

ConcernPractice
N + 1 versionsMaintain current + previous
Shared core logicAvoid duplicate bugs
Test matrixContract tests per version

11. Implementing Version Negotiation

StepDetail
Read version headerAPI-Version or Accept
Default on absentLatest stable or earliest supported
Reject unsupported406 Not Acceptable

12. Implementing Version Sunset Policy

Sunset Sequence

  1. Announce deprecation in changelog + headers
  2. Set Sunset date (≥ 6 months out)
  3. Email/notify active consumers
  4. Track usage; reach out to remaining clients
  5. Return 410 Gone on sunset date