users@jersey.java.net

Re: [Jersey] _at_Consumes based on additional headers (versioning)

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Fri, 30 Jul 2010 17:31:12 +0200

Hi Bernhard,

It is currently not possible to match resource methods to anything
other than the HTTP method and the media that is consumed/produced.

You could do something like the following:

    @POST @Path("/dummy")@Consumes(MediaType.APPLICATION_XML)
    public Object myDummy(@Context HttpRequestContext hc,
@HeaderParam("X-API-Version") int version) {
      if (version == 1) {
        MyRequestV1 v1 = getEntity(MyRequestV1.class);
        return v1;
      } else ...
    }

but it is not very pretty.

Generally it is considered better to version the media type, and if
you still want to keep the media type general to xml, e.g. a
derivative of application/xml for client reasons, it could be say
application/vnd.xxx.yyy.v1+xml, application/vnd.xxx.yyy.v2+xml etc.

Paul.

On Jul 8, 2010, at 5:38 PM, Cybernd wrote:

> Hi,
>
> As far as i understand, jersey finds the suitable resource method
> based on @Path and @Consumes.
> Jersey is also able to do content negotiation based on @Consumes
> quality header properties.
>
> Is there a way to use an additional artificial version header?
>
> The basic idea would be (pseudocode):
>
> @POST @Path("/
> dummy")@Consumes(MediaType.APPLICATION_XML)@ConsumeHeader("X-API-
> Version", "1")
> public MyResponseV1 myDummy(MyRequestV1 request) {}
>
> @POST @Path("/
> dummy")@Consumes(MediaType.APPLICATION_XML)@ConsumeHeader("X-API-
> Version", "2")
> public MyResponseV2 myDummy(MyRequestV2 request) {}
>
> Rational: This would allow me to support both versions running at
> the same time.
> I am aware that it is possible to reach the same behavior with
> custom mediaTypes but my current rest API spec wants to use the
> custom version header.
>
> The easiest workaround would be to use String for request and
> response, but this would mean that i lose the ability to use jerseys
> integrated jaxb/json support.
>
> Spring MVC rest support seems to be able to do something like that
> out of the box.
> It is possible to configure an array of consuming headers using this
> syntax:
> headers={"headername=headervalue"}
>
> Is it possible to reach my goal?
> Or is it a feature that is missing intentional because it is against
> some REST rules?
>
> URI versioning (path and/or query) is in my usecase unfavored.
>
> thanks,
> Bernhard Neuhauser
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>