通过创建抽象类对休息服务进行版本控制? [英] versioning rest service by creating abstract class?

查看:70
本文介绍了通过创建抽象类对休息服务进行版本控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST 服务,版本 v1 在生产中运行良好.现在我需要制作版本 v2 url,因为响应格式已更改,因此我们不想影响使用 v1 url 的当前客户.我们将使用版本 v2 url 返回一些其他对象,而不是使用 ClientResponse 对象.

I have a REST service with version v1 running fine in production. Now I need to make version v2 url as the response format is changed so we don't want to affect our current customers who are using v1 url. We will be returning some other object back with version v2 url instead of using ClientResponse object.

以下是我当前的设计,@Path 注释中提供了该版本.这是由离开我们团队的其他人完成的.

Below is my current design in which version is provided in @Path annotation. This was done by somebody else who left our team.

@Component
@Scope("request")
@Path("/abc/hello/v1")
public class ClientService {

    // ... some variables

    @GET
    @Path("/json/line")
    @Produces(MediaType.APPLICATION_JSON)
    public ClientResponse getLineData(@Context UriInfo uriInfo) {


    }
}

在这里设计版本 v2 url 的最佳方法是什么?我应该创建一个新类并将 @Path 作为 @Path("/abc/hello/v2") 像这样并将所有内容复制粘贴到其中吗?或者我应该创建一些抽象类并让 ClientServiceV1 扩展该抽象类,然后让 ClientServiceV2 也扩展该抽象类?我应该如何进行?

What is the best way to design version v2 url here? Should I just make a new class and have @Path as @Path("/abc/hello/v2") like this and copy paste everything in it? Or should I create some abstract class and have ClientServiceV1 extend that abstract class and then have ClientServiceV2 extend that abstract class as well? How should I proceed?

推荐答案

我对 REST API 进行版本控制的策略是不让 JAX-RS 运行时自动确定要加载的 REST 资源,而是在 java.ws.rs.Application 实现.

My strategy for versioning REST API is to not let JAX-RS runtime automatically determine what REST resources to load and instead explicitly state them in the java.ws.rs.Application implementation.

我的 java.ws.rs.Application 实现是我进行版本控制的地方,我在那里声明了基本 API URI

My java.ws.rs.Application implementation is where I do the versioning and I state it there the base API URI

@javax.ws.rs.ApplicationPath("v1")
public class MyAppV1 extends java.ws.rs.Application {
    Set<Class<?>> getClasses() {
       return new java.util.HashSet<>(java.util.Arrays.asList(
           ClientService.class,
           OtherService.class));
    }
}

然后为v2"创建另一个,我开始在那里添加我的组件.

And then create another one for "v2" where I start adding my components there.

它的目的是我可以有多个版本,我可以弃用旧版本并最终根据需要删除它们.它还允许我重用现有的服务.

The intent of it is I can have multiple versions present and I can deprecate the old ones and eventually remove them as needed. It also allows me to reuse the existing services.

但是,如果您现有的服务后缀为v1",那么您可能需要根据需要复制代码或使其指向新版本.

However, if your existing services are suffixed with "v1" then you may want to either duplicate the code or make it point to the new version depending on your needs.

这篇关于通过创建抽象类对休息服务进行版本控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆