Q1. Why is API versioning important?
API versioning allows you to make changes to your API without breaking existing clients.
As your API evolves, you may add new fields, change behavior, or deprecate old endpoints.
Versioning ensures backward compatibility.
As your API evolves, you may add new fields, change behavior, or deprecate old endpoints.
Versioning ensures backward compatibility.
Q2. What are the common approaches to API versioning?
Common approaches:
• URI path versioning (
• Query parameter versioning (
• Header versioning (
• Content negotiation (Accept header with version)
Each has pros and cons.
• URI path versioning (
/v1/users)• Query parameter versioning (
/users?version=1)• Header versioning (
Accept: application/vnd.myapp.v1+json)• Content negotiation (Accept header with version)
Each has pros and cons.
Q3. How do you implement URI versioning in Spring?
Simply include version in the URL mapping.
Example:
Example:
@GetMapping("/v1/users")
public List getUsersV1() { ... }
@GetMapping("/v2/users")
public List getUsersV2() { ... }
Q4. How do you implement header versioning?
Use the
Example:
headers attribute of @RequestMapping.Example:
@GetMapping(value = "/users", headers = "X-API-Version=1")
public List getUsersV1() { ... }
Q5. How do you implement content negotiation versioning?
Use the
Example:
produces attribute with custom media type.Example:
@GetMapping(value = "/users", produces = "application/vnd.myapp.v1+json")
public List getUsersV1() { ... }
