LazyGuy
LazyGuy

Reputation: 711

How to do REST webservice url versioning?

I need to version a REST web service API. I have gone through several posts under Stack Overflow. I discovered that there are three approaches: Url versioning, Custom Request header and Accept Header. I was about to go for URL versioning.

My URL is something like this:

 http://localhost:8080/api/svcs/1.0/usrLocation?1234

Now my question how to version the above url. I didn't find any practical example from server side code. I was able to find the theoretical and URL like:

http://localhost:8081/api/svc/v1/ and blah blah..

How will my jax-rs implementation code will look like and how will the actual concept will be implemented in server side?

I am a beginner to this. My JAX-RS implementation is Apache CXF

My code:

  @Get
  @Path("/usrLocation")
   @Produces (MediaType.APPLICATION_JSON)
  public Response getUsrLocation(@QueryParam ("locId") String locId){
     //  logic
   }

How the url http://localhost:8080/api/svcs/1.0/usrLocation?1234 needs to be changed to versioning so that it will be helpful in future and how it works from jax-rs implementation side?

Upvotes: 1

Views: 257

Answers (1)

Karthik Prasad
Karthik Prasad

Reputation: 10004

You can follows two design approach

  1. Say suppose your changes( would be) are minimal and its at particular part of code, for example you have insurance policy application and you are upgrading the application to new version because govt has introduced a policy which effects commissioning. and you have module for calculating commissioning then you can pass the version as a parameter.

    @Path("/svc")
    public class KPApiService {
    
    private final static Logger LOG = LoggerFactory.getLogger(KPApiService.class);
    
    @Path("/{version}/myapp")
    @GET
    public Response myApp(@PathParam("version")String version, @QueryParam("kpdata")String data){
        LOG.debug("You and entered version {} and data {} ", version, data);
    }
    }
    
  2. Say suppose you are changes are effecting so much and you want to introduce new module but there are few users(legacy) who are yet to brass the changes, then you can create a separate method with different version

    @Path("/svc")
    public class KPApiService {
    
    private final static Logger LOG = LoggerFactory.getLogger(KPApiService.class);
    
    @Path("/1.0/myapp1")
    @GET
    public Response myApp1(@QueryParam("kpdata")String data){
        LOG.debug("You and entered version v1 and data {} ", data);
    }
    
    @Path("/1.2/myapp1")
    @GET
    public Response myApp2(@QueryParam("kpdata")String data){
        LOG.debug("You and entered version v2 and data {} ", data);
    }
    
    
    }
    

Upvotes: 2

Related Questions