Spring rest api versioning

In my spring controller I have 2 rest api methods. example: getUser, getRole One client addresses it as "/ api / v1". Now I want to update one of the methods. those. getRole. So the new method / version will be "/ api / v2". But no changes to v1. those. "/ api / v1".

How to handle break methods from both versions in the same project? I mean, the getUser rest API should support both "/ api / v1" and "/ api / v2". And getRole rest API should support both versions, but different functionality (ex: database changed, logic changed). In simple terms, 1. getUser will have 1 method which supports both versions. 2. getRole will have 2 methods for each version.

Please help me here.

+3


source to share


4 answers


If you want to use a separate method for both versions, you can define a different value in @RequestMapping

method 1

    @RequestMapping(
                value = "baseurl/v1/role",
                method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_VALUE)

   and


    @RequestMapping(
                    value = "baseurl/v2/role",
                    method = RequestMethod.GET,
                    produces = MediaType.APPLICATION_JSON_VALUE)

      

but if you want to handle it in the same method you can use

method 2: use @PathVariable

@RequestMapping(
            value = "baseurl/{version}/role",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
public returnType methodName(@PathVariable("version") String version){

// code to check version

}

      



method 3: use @RequestHeader

@RequestMapping(
                value = "baseurl/role",
                method = RequestMethod.GET,
                produces = MediaType.APPLICATION_JSON_VALUE)
    public returnType methodName(@RequestHeader("version") String version){

    // code to check version

    }

      

But as you said you have a different api version, prefer to manage them in a separate controller using @RequestMapping

at the class level

method 4:

@RestController
@RequestMapping("/v1")
public class anyController {

}

and 

@RestController
@RequestMapping("/v2")
public class anyController {

}

      

+1


source


It really depends on your use case and what is changing. There are many different paths you can take, but I will describe two that sound like they will work for you.

  • Create a path parameter for v1 and add a condition that checks if the path parameter is v2 and calls another method.

  • Create a new path for api / v2, add change functions and call the v1 endpoint method.



This is indeed specific and should probably be judged in what way you should accept it, depending on how the existing code is being implemented.

+1


source


I would take the following approach

Determine the version at the application level (Start with / v 1) - this version changes only when you want to make big changes for the whole API and is usually stable.

Resources (roles, users), etc. must be versioned using content negotiation via headers

eg.

GET /v1/users/503deb67-ff6e-4d1c-a225-408b910b7252/ HTTP/1.1
Accept: application/vnd.yourorg.users-v1+json

      

Here, the client uses content negotiation for a specific resource that interests him via an HTTP header

See https://www.mashery.com/blog/ultimate-solution-versioning-rest-apis-content-negotiation and http://blog.ploeh.dk/2015/06/22/rest-implies-content- negotiation / for more details

+1


source


There are many ways, but most likely you want the code to be as decoupled as possible. This makes keeping versions in separate classes a good option:

V1Controller.java:

@RestController
@RequestMapping("/api/v1")    
public class V1Controller{
    // version 1 api
}

      

V2Controller.java:

@RestController
@RequestMapping("/api/v2")    
public class V2Controller{
    // version 2 api
}

      

But for an even higher degree of decoupling, you could have two different web applications deployed on the same Tomcat or Jetty server - version 1 in the context of the "/ api / v1" path and version 2 in the context of "/ api / v2". Webapps will just build different versions of the same code (or builds from different branches - if you're using git).

0


source







All Articles