Jersey 使用accept标头对资源进行restlet版本控制
如何在jersey中尽可能使用restlet使用accept header字段对REST api进行版本控制:Jersey 使用accept标头对资源进行restlet版本控制,jersey,versioning,restlet,Jersey,Versioning,Restlet,如何在jersey中尽可能使用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")
// 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);
}
为此,您需要为两个版本定义自定义扩展。这可以在您的应用程序中完成:
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");
}
有关更多详细信息,请参见此问题: