Rüdiger Gubler
Rüdiger Gubler

Reputation: 41

restlet versioning of resources using accept header

How can I versioning our REST api using the accept header field using restlet as possible in jersey:

// Jersey
@GET
@Path("test")
@Produces("application/myapp.v1+json;charset=utf-8")
public pojo.v1.PersonPoJo testV1()
{
  return new pojo.v1.PersonPoJo("Smith", "Jeff", 34);
}

@GET
@Path("test")
@Produces("application/myapp.v2+json;charset=utf-8")
public pojo.v2.PersonPoJo testV2()
{
  return new pojo.v2.PersonPoJo("Smith", "Jeff", 34, "[email protected]");
}


// Restlet
@GET("json")
public pojo.PersonPoJo test()
{
  return new pojo.PersonPoJo("Smith", "Jeff", 34);
}

Upvotes: 1

Views: 91

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202156

To do that you need to define custom extensions for your two versions. This can be done within your application:

public class MyApplication extends Application {
    public MyApplication() {
        getMetadataService().addExtension(
            "myapp.v1", new MediaType("application/myapp.v1+json"));
        getMetadataService().addExtension(
            "myapp.v2", new MediaType("application/myapp.v2+json"));
        (...)
    }

    @Override
    public Restlet createInboundRoot() {
        (...)
    }
}

Then you can use these extensions directly within annotations of server resources:

@Get("myapp.v1")
public pojo.v1.PersonPoJo testV1()
{
  return new pojo.v1.PersonPoJo("Smith", "Jeff", 34);
}

@Get("myapp.v2")
public pojo.v2.PersonPoJo testV2()
{
  return new pojo.v2.PersonPoJo("Smith", "Jeff", 34, "[email protected]");
}

See this question for more details:

Upvotes: 1

Related Questions