Back to: Spring Boot Tutorials
Spring Boot HTTP Header Versioning
In this article, I am going to discuss Spring Boot HTTP Header Versioning with Examples. Please read our previous article where we discussed Spring Boot Request Parameter Versioning with Examples.
What is HTTP Header Versioning?
Using HTTP headers is another method to implement versioning in APIs. This method involves specifying the version number in the HTTP request header. When making a request to the API, the client includes a custom header such as ‘Accept-version’ with the desired version number. The server reads this header and returns the corresponding version of the API.
For instance, if a client wants to access version 2 of an API, it would include the following header in its request: ‘Accept-version: 2’. The server processes the request and returns version 2 of the API.
This method is easy to implement and use and offers more flexibility in specifying and accessing versions. However, it requires clients to include an additional header in their requests, which may not always be convenient.
Implementing HTTP Header Versioning in Spring Boot
Let us use the same project as before. The project should have the following class files:
- StudentV1.java
- StudentV2.java
- StudentVersionController.java
- UriversioningApplication.java
As of now, the application is versioned using Request Parameters. We have to change it to be header versioned.
Step 1: Modify StudentVersioningController.java.
Currently, the contents of StudentVersioningController.java should look like this:
Modify the file to:
We have modified the file to take headers, rather than parameters. If the “X-API-VERSION=1” header is received, the getStudentV1() function is executed. Similarly, if the “X-API-VERSION=1” header is received the getStudentV2() function is executed.
Step 2: Compile and execute the application. Ensure compilation is successful.
Step 3: Send a GET request to the URL http://localhost:8080/student via Postman. Remember to add the header such that version 1 is used. You should see the following reply:
As can be seen, this returns an object of type StudentV1. We know this because the name is together (and not split into firstName, middleName, and lastName).
Step 4: Send a GET request to the URL http://localhost:8080/student via Postman. Remember to add the header such that version 2 is used. You should see the following reply:
As can be seen, this returns an object of type StudentV2. We know this because the name is split into firstName, middleName, and lastName. Congratulations! You now know how to implement HTTP header versioning in Spring Boot.
The Complete Example Code
StudentV1.java
package com.dotnet.uriversioning; public class StudentV1 { private String name; private int id; private String email; public String getName() {return name;} public void setName(String name) {this.name = name;} public int getId() {return id;} public void setId(int id) {this.id = id;} public String getEmail() {return email;} public void setEmail(String email) {this.email = email;} public StudentV1() {} public StudentV1(String name, int id, String email) { this.name = name; this.id = id; this.email = email; } }
StudentV2.java
package com.dotnet.uriversioning; public class StudentV2 { private String firstName; private String middleName; private String lastName; private int id; private String email; public String getFirstName() {return firstName;} public void setFirstName(String firstName) {this.firstName = firstName;} public String getMiddleName() {return middleName;} public void setMiddleName(String middleName) {this.middleName = middleName;} public String getLastName() {return lastName;} public void setLastName(String lastName) {this.lastName = lastName;} public int getId() {return id;} public void setId(int id) {this.id = id;} public String getEmail() {return email;} public void setEmail(String email) {this.email = email;} public StudentV2() {} public StudentV2(String firstName, String middleName, String lastName, int id, String email) { this.firstName = firstName; this.middleName = middleName; this.lastName = lastName; this.id = id; this.email = email; } }
StudentVersioningController.java
package com.dotnet.uriversioning; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentVersioningController { @GetMapping(value = "/student", headers="X-API-VERSION=1") public StudentV1 getStudentPV1 () { return new StudentV1("Student Number 1", 1, "student.no.1@uni.com"); } @GetMapping(value = "/student", headers="X-API-VERSION=2") public StudentV2 getStudentPV2 () { return new StudentV2("Student", "Number", "1", 1, "student.no.1@uni.com"); } }
UriversioningApplication.java
package com.dotnet.uriversioning; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UriversioningApplication { public static void main(String[] args) { SpringApplication.run(UriversioningApplication.class, args); } }
In the next article, I am going to discuss Spring Boot Security with Examples. Here, in this article, I try to explain Spring Boot HTTP Header Versioning with Examples. I hope you enjoy this Spring Boot HTTP Header Versioning article.