使用 Maven 配置 swagger-ui

zap*_*pee 10 java jax-rs swagger swagger-ui swagger-2.0

我正在将我的 JAX-RS REST 项目与 Swagger 集成。我阅读了许多文档和教程,我最喜欢的图如下(感谢Philipp Hauer 的博客):

在此处输入图片说明

该图像帮助我了解 Swagger 的工作原理。

在了解 Swagger 的工作原理后,我修改了我的pom.xml.

swagger-jersey2-jaxrs向我的项目添加了依赖项,这使我能够使用此处描述的与 swagger 相关的注释:

<!-- for Swagger-Core Annotations -->
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jersey2-jaxrs</artifactId>
    <version>1.5.13</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

重要提示:我无法使用最新的 1.15.18 swagger-jersey2-jaxrs 依赖项,因为属于此依赖项的番石榴库在最新的 (v5.181) Payara应用程序服务器上造成了严重的类加载器问题:

  Exception Occurred :Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.NoSuchMethodError: com.google.common.collect.Sets$SetView.iterator()Lcom/google/common/collect/UnmodifiableIterator;. Please see server.log for more details. ]]
Run Code Online (Sandbox Code Playgroud)

无论如何,将以下插件添加到我的 pom.xml 以及下载swagger-ui部分并将其解压缩到 maven 目标文件夹:

<plugin>
    <groupId>com.googlecode.maven-download-plugin</groupId>
    <artifactId>download-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>swagger-ui</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>wget</goal>
            </goals>
            <configuration>
                <url>https://github.com/swagger-api/swagger-ui/archive/v${version.swagger-ui}.tar.gz</url>
                <unpack>true</unpack>
                <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我的问题是我在代理服务器后面,所以我必须将 HTTPS 代理配置添加到 maven setting.xml。

最后,我添加maven-war-plugin到 pom.xml 中,将与 swagger-ui 相关的静态文件复制到我的最终 war 文件中:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <warName>${project.build.finalName}</warName>
        <webappDirectory>${basedir}/target/${project.build.finalName}</webappDirectory>
        <webResources>
            <webResource>
                <directory>${project.build.directory}/swagger-ui-${version.swagger-ui}/dist</directory>
                <targetPath>swagger</targetPath>
            </webResource>
        </webResources>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我使用以下代码初始化 swagger 文档生成器:

@ApplicationPath("api")
public class Configurator extends Application {
    public Configurator() {
        BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion("1.0.0");
        beanConfig.setSchemes(new String[]{"http"});
        beanConfig.setHost("localhost:8080");
        beanConfig.setBasePath("my-rest-1.0.0/api");
        beanConfig.setResourcePackage(EchoRest.class.getPackage().getName());
        beanConfig.setTitle("JAX-RS + Swagger and Swagger UI Example");
        beanConfig.setDescription("Sample RESTful API built using JAX-RS, Swagger and Swagger UI");
        beanConfig.setScan(true);
    }
}
Run Code Online (Sandbox Code Playgroud)

它工作正常,http://localhost:8080/my-rest-1.2.0/api/swagger.json返回一个适当的 swagger 文档。

  1. 我需要将swagger/index.html文件的 JavaScript 部分中的硬编码 url 值从默认的http://petstore.swagger.io/v2/swagger.json覆盖到我的 URL。我通过 Maven 插件自动下载和复制 swagger-ui,但我不知道如何在使用 Maven 编译期间自动更改 URL 变量的值。
  2. 我的 WAR 文件的名称取决于 maven pom.xml 中使用的版本信息。这意味着类中使用的硬编码版本、主机和基本路径Configurator在一段时间后将不正确。我可以根据应用程序根 url 和 @ApplicationPath("api") 注释中的值自动生成这些值吗?

小智 5

特别是为了指出您的需求之一,我们在 Maven 中使用替换插件来替换硬编码的 url:

<!-- replace name of the specification file to show-->
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/index.html</file>
                    <replacements>
                        <replacement>
                            <token>"https://petstore.swagger.io/v2/swagger.json"</token>
                            <value>location.protocol + '//' + location.hostname+':'+location.port+'/${project.artifactId}-${project.version}/Your-URL/openapi.json'</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>
Run Code Online (Sandbox Code Playgroud)