如何在泽西时使用restlet使用accept header字段对REST api进行版本控制:
// 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, "j.smith@rest.com");
}
// Restlet
@GET("json")
public pojo.PersonPoJo test()
{
return new pojo.PersonPoJo("Smith", "Jeff", 34);
}
答案 0 :(得分:1)
为此,您需要为两个版本定义自定义扩展。这可以在您的申请中完成:
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() {
(...)
}
}
然后,您可以直接在服务器资源的注释中使用这些扩展:
@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, "j.smith@rest.com");
}
有关详细信息,请参阅此问题: