java - 打包到uber jar后Swagger不工作

标签 java eclipse maven swagger jersey-2.0

我有一个 Jersey 应用程序在嵌入式 Grizzly 中运行,并挂接了 Swagger。当我从 Eclipse 运行应用程序时,Swagger 工作正常。 URL http://localhost:8080/myapp/api-docs 返回预期的 JSON 响应

{
"apiVersion": "1.0.0",
"swaggerVersion": "1.2",
"apis": [
    {
        "path": "/myresource",
        "description": "Hello my resource description"
    }
]}

但是,将应用程序打包到单个 jar 并运行生成的 jar 后,Swagger 停止工作。访问相同的 URL http://localhost:8080/myapp/api-docs 现在会返回 HTTP 状态 500。应用程序本身工作正常 - 我可以向 Web 服务发出请求。

这是我用来打包应用程序的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>simple-service</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>simple-service</name>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-grizzly2-http</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
        </dependency>
        <dependency>
            <groupId>com.wordnik</groupId>
            <artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
            <version>1.3.12</version>
        </dependency>
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.0.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <createDependencyReducedPom>true</createDependencyReducedPom>
                    <filters>
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>META-INF/*.SF</exclude>
                                <exclude>META-INF/*.DSA</exclude>
                                <exclude>META-INF/*.RSA</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>

                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <manifestEntries>
                                        <Main-Class>com.example.Main</Main-Class>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <jersey.version>2.22.1</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

最佳答案

您使用的 swagger 版本非常旧,并且包含对 scala 的依赖等。建议升级到1.5.3,它的依赖列表更小。

很可能您的 jar 文件具有不兼容的 jackson 版本,从而导致了该问题。

带有放置向导的 Swagger 可以毫无问题地构建一个 uber jar。

关于java - 打包到uber jar后Swagger不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33129111/

相关文章:

java - 我们可以放大缩小在 Canvas 上绘制的图形吗?

java - getExternalFilesDir(null) 抛出 NullPointerException

eclipse - 让 Eclipse 识别 Pylint "pylint.rc"文件

eclipse - 在 m2e Eclipse 插件中增加堆大小

java - FXMLLoader 找不到 fxml 文件 - Maven 项目

java - 为团队设置 Maven

Java 1.8 FileOutputStream 创建目录

java - 构建 monodroid 示例 map 应用程序

eclipse - Eclipse+Pydev 中转到定义的[快捷方式/其他方式]是什么

java - 什么是 .m2 文件夹,如果我有两个包含两个不同项目的存储库,我该如何配置它