Category: spring_boot
API Versioning allow
Published on 26 Jun 2026
Explanation
API Versioning allows developers
to introduce
new features or changes without breaking
existing clients.
Common versioning strategies include
URI Versioning,
Request Parameter Versioning, Header
Versioning, and Content Negotiation.
Code:
// Version 1 GET /api/v1/students // Version 2 GET /api/v2/students
Explanation
URI Versioning is the most commonly
used approach. The version number is
included directly in the endpoint URL,
making it easy to understand and
maintain.
Code:
@RestController
@RequestMapping("/api/v1/students")
public class StudentControllerV1 {
@GetMapping
public String getStudents() {
return "Student API Version 1";
}
}
Explanation
Request Parameter Versioning uses a query
parameter to determine which version of
the API should be executed. This
keeps the URL structure unchanged.
Code:
@GetMapping(value = "/students",
params = "version=1")
public String getStudentsV1() {
return "Student API Version 1";
}
@GetMapping(value = "/students",
params = "version=2")
public String getStudentsV2() {
return "Student API Version 2";
}
Explanation
Header Versioning uses custom HTTP headers
to specify the API version. This
approach keeps
URLs clean and separates
version information f
rom resource identifiers.
Code:
@GetMapping(value = "/students",
headers = "X-API-VERSION=1")
public String getStudentsV1() {
return "Student API Version 1";
}
@GetMapping(value = "/students",
headers = "X-API-VERSION=2")
public String getStudentsV2() {
return "Student API Version 2";
}
Explanation
Content Negotiation Versioning uses
the Accept
header to determine the API version.
This is often considered the most
RESTful approach because version
information is
included in the media type.
Code:
@GetMapping(value = "/students",
produces = "application/vnd.hackforge.v1+
json")
public String getStudentsV1() {
return "Student API Version 1";
}
@GetMapping(value = "/students",
produces = "application/vnd.hackforge.v2+
json")
public String getStudentsV2() {
return "Student API Version 2";
}