Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions spring-boot-modules/spring-boot-api-versioning/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Maven build
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties

# IDE files
.idea/
*.iml
*.ipr
*.iws
.vscode/
.project
.classpath
.settings/

# OS files
.DS_Store
Thumbs.db

# Logs
logs/
*.log
99 changes: 99 additions & 0 deletions spring-boot-modules/spring-boot-api-versioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# spring-boot-api-versioning
Demonstrating API versioning strategies in Spring Boot
# API Versioning in Spring Boot

[![Spring Boot](https://img.shields.io/badge/Spring%20Boot-3.3+-brightgreen)](https://spring.io/projects/spring-boot)
[![Java](https://img.shields.io/badge/Java-17+-blue)](https://openjdk.org/)
[![Build](https://img.shields.io/badge/Build-Maven-orange)](https://maven.apache.org/)

A demo project showcasing **different strategies for API versioning in Spring Boot**, including:

- URI Versioning
- Request Parameter Versioning
- Header Versioning
- Content Negotiation (MIME Type)
- Native Spring Boot 4 Annotation Support

---

## Project Setup

Clone the repository:

```bash
git clone https://github.com/your-org/api-versioning-spring-boot
cd api-versioning-spring-boot
```

Build and run:

```bash
./mvnw spring-boot:run
```

---

## Usage Examples

### 1. URI Versioning
```bash
curl http://localhost:8080/api/v1/users
curl http://localhost:8080/api/v2/users
```

### 2. Request Parameter Versioning
```bash
curl http://localhost:8080/api/users?version=1
curl http://localhost:8080/api/users?version=2
```

### 3. Header Versioning
```bash
curl -H "API-VERSION: 1" http://localhost:8080/api/users
curl -H "API-VERSION: 2" http://localhost:8080/api/users
```

### 4. Content Negotiation
```bash
curl -H "Accept: application/vnd.company.v1+json" http://localhost:8080/api/users
curl -H "Accept: application/vnd.company.v2+json" http://localhost:8080/api/users
```

### 5. Native Annotation Support (Spring Boot 4)
```bash
curl http://localhost:8080/api/users --header "API-VERSION: 1"
curl http://localhost:8080/api/users --header "API-VERSION: 2"
```

---

## Tutorial Reference

This project accompanies the post:
**[API Versioning in Spring](#)**
- (link will be added once published)
---

## Running Tests

```bash
./mvnw test
```

---

## Best Practices

- Version only when necessary
- Document changes clearly
- Deprecate gracefully
- Automate testing across versions
- Align with business needs

---

## 📜 License

This project is licensed under the MIT License.

---
73 changes: 73 additions & 0 deletions spring-boot-modules/spring-boot-api-versioning/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.baeldung.example</groupId>
<artifactId>spring-boot-api-versioning</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-api-versioning</name>
<description>API versioning strategies in Spring Boot</description>

<properties>
<java.version>17</java.version>
<spring.boot.version>3.3.4</spring.boot.version>
</properties>

<!-- Import the Spring Boot BOM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<!-- Web starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Actuator starter (for /actuator/mappings etc.) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Test starter (JUnit 5, Spring Boot Test, Mockito, etc.) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.baeldung.example.apiversioning;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApiVersioningDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ApiVersioningDemoApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.example.apiversioning.controller;

import com.baeldung.example.apiversioning.model.UserV2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserParamController {

@GetMapping("/api/users")
public Object getUsers(@RequestParam(name = "version", defaultValue = "1") String version) {
if ("1".equals(version)) {
return List.of("Alice", "Bob");
} else if ("2".equals(version)) {
return List.of(
new UserV2("Alice", "alice@example.com", 30),
new UserV2("Bob", "bob@example.com", 25)
);
} else {
return "Unsupported API version";
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// src/main/java/.../controller/header/UserHeaderController.java
package com.baeldung.example.apiversioning.controller.header;

import com.baeldung.example.apiversioning.model.UserV1;
import com.baeldung.example.apiversioning.model.UserV2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserHeaderController {

@GetMapping(value = "/api/users", headers = "X-API-VERSION=1")
public List<UserV1> getUsersV1() {
return List.of(new UserV1("Alice"), new UserV1("Bob"));
}

@GetMapping(value = "/api/users", headers = "X-API-VERSION=2")
public List<UserV2> getUsersV2() {
return List.of(
new UserV2("Alice", "alice@example.com", 30),
new UserV2("Bob", "bob@example.com", 25)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// src/main/java/com/baeldung/example/apiversioning/controller/mime/UserMimeController.java
package com.baeldung.example.apiversioning.controller.mime;

import com.baeldung.example.apiversioning.model.UserV1;
import com.baeldung.example.apiversioning.model.UserV2;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserMimeController {

public static final String V1_MEDIA = "application/vnd.example.users-v1+json";
public static final String V2_MEDIA = "application/vnd.example.users-v2+json";

@GetMapping(value = "/api/users", produces = V1_MEDIA)
public List<UserV1> usersV1() {
return List.of(new UserV1("Alice"), new UserV1("Bob"));
}

@GetMapping(value = "/api/users", produces = V2_MEDIA)
public List<UserV2> usersV2() {
return List.of(
new UserV2("Alice", "alice@example.com", 30),
new UserV2("Bob", "bob@example.com", 25)
);
}

// Optional fallback
@GetMapping(value = "/api/users", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UserV1> defaultUsers() {
return List.of(new UserV1("Alice"), new UserV1("Bob"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.example.apiversioning.controller.negotiation;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/users")
public class UserContentNegotiationController {

@GetMapping(value = "/negotiation", produces = "application/vnd.col.users.v1+json")
public String getUsersV1() {
return "User list v1";
}

@GetMapping(value = "/negotiation", produces = "application/vnd.col.users.v2+json")
public String getUsersV2() {
return "User list v2";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.example.apiversioning.controller.v1;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/users")
public class UserV1Controller {
@GetMapping
public String getUsersV1() {
return "User list from API v1";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.example.apiversioning.controller.v2;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v2/users")
public class UserV2Controller {
@GetMapping
public String getUsersV2() {
return "User list from API v2 with extra fields";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.example.apiversioning.model;

public class UserV1 {
private String name;
public UserV1() {}
public UserV1(String name) { this.name = name; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.example.apiversioning.model;

public class UserV2 {
private String name;
private String email;
private int age;

public UserV2(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}

// getters
public String getName() { return name; }
public String getEmail() { return email; }
public int getAge() { return age; }
}
Loading