找不到MessageBodyWriter,从fatjar运行时出错

问题描述 投票:1回答:1

仅当从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>

Shade插件配置:

<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>

Jersey版本是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个回答
0
投票

尝试在代码中显式加载所需的依赖项,而不依赖自动发现,将其注册到Jersey。

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

我认为有一种方法可以强制将它们包含在插件配置中,但是我不记得该配置是从内存中获取的,您必须在文档中进行搜索。

© www.soinside.com 2019 - 2024. All rights reserved.