Chris Jones - Belgium
Chris Jones - Belgium

Reputation: 187

Rest Versioning with Mime Types - Default Version for */*

I have implemented versioning on my REST services with CXF by defining a vendor mime type which works fine when the correct accept header is passed.

However, when a client does not pass an accept header but asterix/asterix my service defaults to returning the first version of the service.

Okay, this is correct as the client has specified that they accept anything but I would like it to default to the second latest version of the service.

Is there a way to specify that this service is the default one that should be used in this case?

Thanks

Service One Annotation:

@GET
@Path("/")
@Produces(value = {CustomMediaType.APPLICATION_CUSTOM_MEDIATYPE_JSON_V1})

Service Two Annotation:

@GET
@Path("/")
@Produces(value = {MediaType.APPLICATION_JSON,CustomMediaType.APPLICATION_CUSTOM_MEDIATYPE_JSON_V2})

Upvotes: 3

Views: 518

Answers (2)

François Gautier
François Gautier

Reputation: 202

The solution below only would works for maintaining 2 versions at the same time:

Remove the @Produces of the older version (v1).

This will force the use of WildCard on the v1 which (therefore) will be less specific than your @Produce in v2.

  • Without MediaType, the dispatcher will pick up the most specific method (therefore, version 2).
  • With MediaType Version 1, the only match would be the wild card => getObjectV1.
  • With MediaType Version 2, there is two matches (v1 and v2) but v2 is more specific => getObjectV2

    @GET
    @Path("/")
    public Object getObjectV1(){
    }
    
    @GET
    @Path("/")
    @Produces(value ={CustomMediaType.APPLICATION_CUSTOM_MEDIATYPE_JSON_V2})
    public Object getObjectV2(){
    }

http://bill.burkecentral.com/2013/05/29/the-poor-jax-rs-request-dispatching-algorithm/

Upvotes: 1

user1907906
user1907906

Reputation:

Just add */*:

@Produces(value = {MediaType.WILDCARD,
                   MediaType.APPLICATION_JSON,
                   CustomMediaType.APPLICATION_CUSTOM_MEDIATYPE_JSON_V2})

Upvotes: 0

Related Questions