1 Versioning Rest Api URI

 Versioning Rest Api:

  • Suppose you have built an great Rest API
    • You have 100s of cutomers
    • Now you need to implemnt a breaking change
      • Example: spltit the name into two firstName and LastName
with Api
localhost:8080/v1/person

{
    name: "irfan khan"
}

with Api
localhost:8080/v2/person

{
    "name"; {
            firstName: "irfan",
            lastName: "Khan"
    }
}

So in given above examples you can see with two different Response ,
so we can not directly implement this change into production in first api itself other wise it will hit to the other list of consumer who would want response in single name instead of split name in firstname and lastName, than all you consumer will impacted.

Solution : of given above problem is Versioning Rest API

So There are number of ways to implements versioning:
1. Url
2. Request Parameter
3. Header
4. Media Type



1.Url
first version of Person v1

Step 1:VersioningPersonController.java

@RestController

public class VersioningPersonController {

@GetMapping("/v1/person")

public Personv1 getFirstVersionOfPerson() {

return new Personv1("Irfan Khan");

}

}


Step 2: Personv1.java

public class Personv1 {

private String name;

public Personv1(String name) {

super();

this.name = name;

}

public String getName() {

return name;

}

@Override

public String toString() {

return "Personv1 [name=" + name + "]";

}

}


now run in either browser or from your Rest Client





Person v2
Step1: VersioningPersonController.java

@RestController

public class VersioningPersonController {

@GetMapping("/v2/person")

public Personv2 getSecondVersionOfPerson() {

return new Personv2(new Name("Irfan", "Khan"));

}

}


Step2: Name.java

public class Name {

private String firstName;

private String lastName;

public Name(String firstName, String lastName) {

super();

this.firstName = firstName;

this.lastName = lastName;

}

public String getFirstName() {

return firstName;

}

public String getLastName() {

return lastName;

}

@Override

public String toString() {

return "Personv2 [firstName=" + firstName + ", lastName=" + lastName + "]";

}

}


Step 3: Personv2.java

public class Personv2 {

private Name name;


public Personv2(Name name) {

super();

this.name = name;

}


public Name getName() {

return name;

}


@Override

public String toString() {

return "Personv2 [name=" + name + "]";

}

}


with given 3 classes now we have splited the name into firstname
lastName




in given above example we have done the versioning through URLS

URL Versioning
  •     http://localhost:8080/v1/person
  •     http://localhost:8080/v2/person

                                        



Comments

Popular posts from this blog

Introduction of RESTful Web Service

Learn JPA and Hibernate

Implementing Dynamic Filtering for Rest API