Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jersey 使用accept标头对资源进行restlet版本控制_Jersey_Versioning_Restlet - Fatal编程技术网

Jersey 使用accept标头对资源进行restlet版本控制

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中尽可能使用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);
}

为此,您需要为两个版本定义自定义扩展。这可以在您的应用程序中完成:

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");
}
有关更多详细信息,请参见此问题: