简体   繁体   English

多个Swagger端点用于API版本控制

[英]Multiple Swagger endpoints for API versioning

I am working with the following technologies: 我正在使用以下技术:

  • Java 7 Java 7
  • Tomcat7 雄猫7
  • JAX-RS (Jersey 2) JAX-RS(泽西岛2)
  • Swagger 2 昂首阔步2
  • Spring 4.3.3.RELEASE 春天4.3.3发布

Where my backend hosts various types of versioned REST APIs. 我的后端托管各种类型的版本化REST API。 We have a v1 and v2 REST API context base path (/api/v1 and /api/v2). 我们有一个v1和v2 REST API上下文基本路径(/ api / v1和/ api / v2)。 The way that we host these two different API versions is via two different Jersey resource configurations (two different servlets). 我们托管这两个不同API版本的方式是通过两个不同的Jersey资源配置(两个不同的servlet)。 This all in one web application deployed to our web application server (we use Tomcat7) as a WAR file. 这一切都作为一个WAR文件部署到我们的Web应用程序服务器(我们使用Tomcat7)中的一个Web应用程序中。

Basic example of what we do: 我们所做的基本示例:

@ApplicationPath("/api/v2")
@Configuration
public class RestApiV2JerseyConfig extends ResourceConfig {

    public RestApiV2JerseyConfig() {
        ...
        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
    }

    @Bean
    public void swaggerV2Bean() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setBasePath("/api/v2/);
        beanConfig.setResourcePackage("com.example.rest.v2");
        beanConfig.setScan(true);
        return beanConfig;
    }

}

@ApplicationPath("/api/v1")
@Configuration
public class RestApiV1JerseyConfig extends ResourceConfig {

    public RestApiV1JerseyConfig() {
        ... 
        register(ApiListingResource.class);
        register(SwaggerSerializers.class);
    }

    @Bean
    public void swaggerV1Bean() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setBasePath("/api/v1/);
        beanConfig.setResourcePackage("com.example.rest.v1");
        beanConfig.setScan(true);
        return beanConfig;
    }


}

Each REST API is registered accordingly in the various configurations above. 每个REST API均在上述各种配置中进行了相应注册。

Now, what I want to be able to do is host Swagger for each of these servlets. 现在,我想要做的是为每个servlet托管Swagger。 This is not possible, am I right? 这是不可能的,对吗? When I tried to do this, the Swagger configurations would overwrite each other, and each endpoint would return the same thing. 当我尝试执行此操作时,Swagger配置将相互覆盖,并且每个端点将返回相同的内容。 Meaning, https://ip-address/api/v1/swagger.json , and https://ip-address/api/v2/swagger.json return the EXACT same response (either the v1 or v2 JSON, depending on which was registered first). 意思是, https://ip-address/api/v1/swagger.jsonhttps://ip-address/api/v2/swagger.json返回完全相同的响应(v1或v2 JSON,具体取决于哪个先注册)。

So what it seems is happening is that the endpoint can be successfully hosted for each servlet, but the content of what is hosted cannot be unique per servlet. 因此,似乎正在发生的事情是可以为每个servlet成功地托管端点,但是托管的内容对于每个servlet来说都不是唯一的。 Or, I might've just messed something up small and haven't noticed yet. 或者,我可能只是把一些小东西弄乱了,还没有注意到。

Version of various things: 各种事物的版本:

  • swagger-jersey2-jaxrs 1.5.2-M2 swagger-jersey2-jaxrs 1.5.2-M2
  • jersey 2.23.1 球衣2.23.1

What am I missing here? 我在这里想念什么? This is fairly easy to reproduce with the code snippets I provided. 通过我提供的代码片段,这很容易重现。 Has anyone else been able to successfully do what I am trying to do? 有没有其他人能够成功完成我想做的事情?

Yes, you can do this, but you need to use the web.xml servlet config method (at least, I haven't figured out how to do it another way). 是的,您可以执行此操作,但是您需要使用web.xml servlet config方法(至少,我还没有弄清楚如何使用另一种方法)。

BeanConfig changes: BeanConfig的更改:

in RestApiV1JerseyConfig 在RestApiV1JerseyConfig中

beanConfig.setConfigId("v1");  
beanConfig.setContextId("v1");
beanConfig.setScannerId("v1");

in RestApiV2JerseyConfig 在RestApiV2JerseyConfig中

beanConfig.setConfigId("v2");  
beanConfig.setContextId("v2");
beanConfig.setScannerId("v2");

web.xml (one for each servlet): web.xml(每个servlet一个):

<!-- v1 -->
<init-param>
    <param-name>swagger.scanner.id</param-name>
    <param-value>v1</param-value>
</init-param>
<init-param>
    <param-name>swagger.config.id</param-name>
    <param-value>v1</param-value>
</init-param>
<init-param>
    <param-name>swagger.use.path.based.config</param-name>
    <param-value>v1</param-value>
</init-param>

<!-- v2 -->
<init-param>
    <param-name>swagger.scanner.id</param-name>
    <param-value>v2</param-value>
</init-param>
<init-param>
    <param-name>swagger.config.id</param-name>
    <param-value>v2</param-value>
</init-param>
<init-param>
    <param-name>swagger.use.path.based.config</param-name>
    <param-value>v2</param-value>
</init-param>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM