Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you version a REST API in Spring Boot? स्प्रिंग बूट में REST API को वर्ज़न कैसे करें?

Answer

Common API versioning strategies in Spring Boot include URI versioning (/api/v1/users, /api/v2/users), request parameter versioning (/api/users?version=1), custom header versioning (X-API-Version: 1), and media-type versioning via the Accept header (application/vnd.company.v1+json).

URI versioning is the most common because it's explicit, cacheable, and easy to route with @RequestMapping("/api/v1/...") on separate controller classes; header and media-type versioning keep URLs stable but require more setup with custom request-mapping conditions.

@RestController
@RequestMapping("/api/v1/users")
public class UserControllerV1 { ... }

@RestController
@RequestMapping("/api/v2/users")
public class UserControllerV2 { ... }

स्प्रिंग बूट में सामान्य API वर्ज़निंग रणनीतियों में URI वर्ज़निंग (/api/v1/users), रिक्वेस्ट पैरामीटर वर्ज़निंग, कस्टम हेडर वर्ज़निंग (X-API-Version: 1), और Accept हेडर के ज़रिए मीडिया-टाइप वर्ज़निंग शामिल हैं।

URI वर्ज़निंग सबसे आम है क्योंकि यह स्पष्ट, कैशेबल है और अलग-अलग कंट्रोलर क्लासेज़ पर @RequestMapping("/api/v1/...") से रूट करना आसान है; हेडर और मीडिया-टाइप वर्ज़निंग URL को स्थिर रखते हैं लेकिन इनके लिए अधिक सेटअप चाहिए।

@RestController
@RequestMapping("/api/v1/users")
public class UserControllerV1 { ... }

@RestController
@RequestMapping("/api/v2/users")
public class UserControllerV2 { ... }

Was this answer clear?