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.
Q2. What are the common approaches to API versioning?
Common approaches: URI path versioning (/v1/users), query parameter versioning (/users?version=1), header versioning (Accept: application/vnd.myapp.v1+json), and 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:
@GetMapping("/v1/users")
public List getUsersV1() { ... }
@GetMapping("/v2/users")
public List getUsersV2() { ... }
Q4. How do you implement header versioning?
Use the 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 produces attribute with custom media type. Example:
@GetMapping(value = "/users", produces = "application/vnd.myapp.v1+json")
public List getUsersV1() { ... }
