0

I'd like to have a versioned REST api using an HTTP Request Header:

GET /someResource

Version: 1.0

Now if I have 2 versions of this resource, my JaxRS web application needs to be able to serve responses appropriate to either of the requested versions. As such I would expect the following classes to be part of my classpath:

@Path("/someResource")
public class SomeResourceV1 {
  **snip**
}

and

@Path("/someResource")
public class SomeResourceV2 {
  **snip**
}

Now I'd like to dispatch the request to one of these resource classes depending on the Version header. I'd assume this would be done using a servlet filter. But since I'm using Guice, I think what I'm looking for is a per request injector.

If request has Version 1.0 use injector with bindings for SomeResourceV1 otherwise if request has Version 2.0 use injector with bindings for SomeResourceV2

I think there is a way to get what I want by subclassing GuiceFilter, but I'm not entirely sure. Has anyone tried something like this before?

Matthew Madson
  • 1,643
  • 13
  • 24
  • Would you mind sharing how you went about solving this? – Rahul Dabas Nov 03 '20 at 03:36
  • @RahulDabas It's been quite some time since I tackled this problem. If memory serves, I believe we used something similar to what Xavier suggested, though we leveraged the profile parameter to determine the version of the resource to use. If I were doing it again today, I'd probably deploy 2 separate versions of the app and use an api gateway to select how to route user requests to the correct backing service. Good luck! – Matthew Madson Nov 03 '20 at 06:38

1 Answers1

1

Not exactly what you're asking for, but you could include the version in the request media-type (eg: application/vnd.foo.v1+json - vnd designates a custom vendor type, and +json lets the JAX-RS implementation know that the incoming request entity is JSON format). Then, you can dispatch using the @Consumes annotation on each of your methods (eg: @Consumes("application/vnd.foo.v1+json") vs @Consumes("application/vnd.foo.v2+json") without having to add custom filters.

Xavier Coulon
  • 1,580
  • 10
  • 15
  • Thanks Xavier, I thought about media types but I tend to agree wtih Joe: http://stackoverflow.com/a/11923744/1028367 Moreover, I don't really like the idea of mixing code for 2 versions in the same class. I might accidentally change something on the V1 codepath breaking V1 consumers inadvertently. What i'd really like is to not have V1 bound or exposed as a jaxrs resource if V2 was requested. – Matthew Madson Feb 10 '14 at 19:11