【问题标题】:MessageBodyWriter not found Error when running from fatjar从 fatjar 运行时找不到 MessageBodyWriter 错误
【发布时间】:2020-09-11 16:34:25
【问题描述】:

我仅在从 fatjar 运行时收到此错误,mvn run:java 按预期工作。

SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.example.Greeting, genericType=class com.example.Greeting.

我正在使用 moxy 进行 json 处理。我试过杰克逊,没有任何区别。尝试用灰熊代替码头,同样的问题。

这是我的 pom 依赖项:

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jetty-http</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.test-framework</groupId>
        <artifactId>jersey-test-framework-core</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-jetty</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
</dependencies>

阴影插件配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.3</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>
            <id>shade</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!--<minimizeJar>true</minimizeJar>-->
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.example.http.JettyHttpServer</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

球衣版本是 2.30.1。

谢谢!

编辑

org.glassfish.jersey.internal.spi.AutoDiscoverable 的内容不包括 org.glassfish.jersey.moxy.json.internal.MoxyJsonAutoDiscoverable

org.glassfish.jersey.logging.LoggingFeatureAutoDiscoverable
org.glassfish.jersey.internal.config.ExternalPropertiesAutoDiscoverable

根据this 应该。我该如何解决?

【问题讨论】:

    标签: jax-rs jersey-2.0


    【解决方案1】:

    尝试在代码上显式加载所需的依赖项,而不是依赖自动发现,在 Jersey 上注册它们。

    fat jar 插件会尝试分析您的代码并查看正在使用的内容和未使用的内容,因此它可以排除未使用的引用并制作更小的 jar。自动发现的问题在于,由于没有对某些类的显式引用,因此阴影插件会认为它们没有被使用,因此可以将其删除。

    我认为有一种方法可以强制将它们包含在插件配置中,但我不记得记忆中的配置,您必须在文档中搜索它。

    【讨论】:

    • 注册 MoxyJsonFeature resourceConfig.register(MoxyJsonFeature.class) 后成功了,谢谢!我试图找到一种方法来聚合所有 AutoDiscoverable 但失败了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多