Spring Boot Request Parameter Versioning

Spring Boot Request Parameter Versioning

In this article, I am going to discuss Spring Boot Request Parameter Versioning with Examples. Please read our previous article where we discussed Spring Boot URI Versioning with Examples.

What is Request Parameter Versioning?

Request parameter versioning is a method of API versioning that distinguishes between versions using a request parameter. With this approach, the base URL remains unchanged while version information is conveyed through the request parameter. On the controller side, the corresponding method is called based on the value of the request parameter.

To get a particular version of an API, we can use http://localhost:8080/student?v=1 for version 1 and http://localhost:8080/student?v=2 for version 2.

Implementing Request Parameter Versioning in Spring Boot

To implement the request parameter version in spring boot, we will be using 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 URI versioned. We have to change it to be request parameter versioned.

Step 1: Modify StudentVersioningController.java.

Currently, the contents of StudentVersioningController.java should look like this:

Spring Boot Request Parameter Versioning with Examples

Modify the file to:

Spring Boot Request Parameter Versioning

We have modified the file to take parameters. If the “v=1” parameter is received, the getStudentV1() function is executed. Similarly, if the “v=2” parameter is received the getStudentV2() function is executed.

Step 2: Compile and execute the application. Ensure compilation is successful.

Implementing Request Parameter Versioning in Spring Boot

Step 3: Send a GET request to the URL http://localhost:8080/student?v=1 via Postman. You should see the following reply:

Spring Boot Request Parameter Versioning with Examples

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?v=2 via Postman. You should see the following reply:

Spring Boot Request Parameter Versioning with Examples

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 request parameter 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", params = "v=1")
    public StudentV1 getStudentPV1 ()
    {
        return new StudentV1("Student Number 1", 1, "student.no.1@uni.com");
    }

    @GetMapping(value = "/student", params = "v=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 this article, I am going to discuss Spring Boot HTTP Header Versioning with Examples. Here, in this article, I try to explain Spring Boot Request Parameter Versioning with Examples. I hope you enjoy this Spring Boot Request Parameter Versioning article.

Leave a Reply

Your email address will not be published. Required fields are marked *