title: Best way to handle JAX-RS REST API URI versioning
body: <p>I did my search first in stackoverflow &amp; I was not able to find out any answers related to my question. All I can find was questions related to REST uri design.</p>
  
  <p>My question in on the backend side. 
  Suppose we have two different version of REST uri's</p>
  
  <p><a href="http://api.abc.com/rest/v1/products" rel="noreferrer">http://api.abc.com/rest/v1/products</a></p>
  
  <p><a href="http://api.abc.com/rest/v2/products" rel="noreferrer">http://api.abc.com/rest/v2/products</a></p>
  
  <p>What is the best approach to follow on the backend side (server side code) for proper routing, manageability &amp; reuse of the existing classes across these two set of api's based on version?</p>
  
  <p>I have thought of approach to define resource classes with different @Path annotations for e.g. have a package for v1 &amp; v2 separately &amp; in ProductsResource class of that package, define</p>
  
  <pre><code>    package com.abc.api.rest.v1.products;
      @Path("/rest/v1/products")
      public class ProductsResource {...}
  
      package com.abc.api.rest.v2.products;
      @Path("/rest/v2/products")
      public class ProductsResource {...}
  </code></pre>
  
  <p>&amp; then have the implementation logic based on the versions. The problems with this approach is when we are only changing one particular resource api from the set of api's, we have to copy other classes to the v2 package also. Can we avoid it?</p>
  
  <p>How about to write a custom annotation say @Version &amp; have values of the versions it supports? Now whether it is v1 or v2, both request will go to same resource class.</p>
  
  <p>Say for e.g.</p>
  
  <pre><code>    package com.abc.api.rest.products;
      @Path("/rest/{version: [0-9]+}/products")
      @Version(1,2)
      public class ProductsResource {...}
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>There was a API versioning suggestion by Jarrod to handle version in headers. That's also one way to do it however, I am looking forward for best practices to use when we are following URI based versioning</p>