【问题标题】:restlet versioning of resources using accept header使用接受标头对资源进行 restlet 版本控制
【发布时间】:2016-03-03 12:52:48
【问题描述】:

如何在 jersey 中尽可能使用 restlet 的接受头字段对我们的 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);
}

【问题讨论】:

    标签: jersey versioning restlet


    【解决方案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");
    }
    

    查看这个问题了解更多详情:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-18
      • 2019-05-16
      • 2021-07-03
      • 2011-12-29
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多