Hackforge Academy

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";
}

๐Ÿš€ Learn Spring Boot with real-world projects

๐Ÿ’ก Build REST APIs step by step

๐Ÿง  Improve backend development skills

๐ŸŽฏ Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

๐Ÿ

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
โš›๏ธ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
โ˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community