Apr 20, 2015

How to do Rest API versioning in java?

I have a query in my usecase. Please help me to solve.

This is my scenario :

I have to do API versioning. I am using REST APIs with Restlet framework. I am confused how to implement the versioning in Java Source code.

If my API looks like this : http://localhost:8080/example.com/v2/hello

Then In my source code how to map the version with appropriate functionality?

For example :

If(version == 1) {//Do Version 1 Source Code}

else If(version == 2) {//Do Version 2 Source Code}

else If(version == 3) {//Do Version 3 Source Code} ………..

If I did like this, Then why we are going for Versioning. We can implement Manually Right? And also If I have to add some 100 versions, then the code looks uglier and it becomes lengthier.

I get confused how to map in Source Code. Please Clarify my doubt.

I have read that we can use annotations for handling API versioning. But I don't know what are the annotations available in restlet framework. Please let me know.


Number6 has a good answer, but here's a link to Oracle's Java developer page in case you want additional information:

This is kind of an extensive question and there really multiple ways to do so. You are correct in the sense, that yes, you can do it manually. You are also correct that the code will become long and ugly - not ideal!

Here you will find a lengthy but well worded answer in the top answer for a similar question on Stack Overflow:


I would also suggest using a distributed revision control system like Git to help you with the process. Some of the tools in the following link may be quite useful. Search the site a little more if not as this may not solve your specific issue:


Hope this helps
@Number6: ya.. this stack overflow question is very useful for how to include version in URL. I want to know that, After passing the request with version, In java source code how to map the functionality for the according version. Like, If version is 1, it should map the version 1 functionality, if version is 2 then it should map the version 2 functionlity, like that am asking.
You need to associate a router inside the restlet and also plugin a redirector for the specific version. So if you understand the implementation of the URI, next make a router that extracts that version and attaches it to a router which redirects it for the appropriate code segment.


The only exception is that you are not necessarily using the extractFromQuery as this example shows pulling a keyword from a Google search.
Answer this