You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven编译Jar启动后Jersey解析JSON报错,IDEA运行正常

我之前也碰到过完全一样的问题!根源就在你的maven-shade-plugin配置里,具体来说是这两个点:

1. minimizeJar=true误删了关键类

这个选项会做静态代码分析,把它判定为“未被直接引用”的类删掉,但Jersey处理JSON转String的消息体阅读器是通过SPI(服务提供者接口)动态加载的,静态分析识别不出这种间接依赖,所以打包时这些类被误移除了——IDE里运行依赖完整所以没问题,打包成jar就炸了。

2. 无关的relocation规则可能添乱

你配置里的<relocations>针对的是com.acme.coyote包,这明显不是你的项目包,没必要保留,反而可能干扰类加载路径。


快速解决方法(推荐)

直接修改maven-shade-plugin配置,关闭最小化打包,同时移除无关的relocation规则:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <!-- 关闭最小化,避免误删依赖类 -->
                <minimizeJar>false</minimizeJar>
                <createDependencyReducedPom>true</createDependencyReducedPom>
                <dependencyReducedPomLocation>
                    ${java.io.tmpdir}/dependency-reduced-pom.xml
                </dependencyReducedPomLocation>
                <!-- 移除无关的relocation配置 -->
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.dataCollection.StartDataCollection</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

如果坚持要最小化打包

如果你想继续用minimizeJar=true来减小jar体积,需要明确指定保留Jersey的JSON处理类和SPI配置文件:

<configuration>
    <minimizeJar>true</minimizeJar>
    <createDependencyReducedPom>true</createDependencyReducedPom>
    <dependencyReducedPomLocation>
        ${java.io.tmpdir}/dependency-reduced-pom.xml
    </dependencyReducedPomLocation>
    <filters>
        <!-- 保留jersey-json里的JSON Provider类和服务配置 -->
        <filter>
            <artifact>com.sun.jersey:jersey-json</artifact>
            <includes>
                <include>com/sun/jersey/json/impl/provider/entity/**</include>
                <include>META-INF/services/**</include>
            </includes>
        </filter>
        <!-- 保留jersey-client的核心客户端类 -->
        <filter>
            <artifact>com.sun.jersey:jersey-client</artifact>
            <includes>
                <include>com/sun/jersey/api/client/**</include>
                <include>META-INF/services/**</include>
            </includes>
        </filter>
    </filters>
    <transformers>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
            <mainClass>com.dataCollection.StartDataCollection</mainClass>
        </transformer>
    </transformers>
</configuration>

验证方法

修改完配置后,执行mvn clean package重新打包,然后运行生成的fat jar,调用REST API的时候应该就能正常解析JSON为String了。

另外,你也可以临时修改API调用代码绕开这个问题(但还是建议从打包配置上解决根源):

Client client = Client.create();
WebResource webResource = client.resource("https://...(correct address)...");
ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
// 直接读取输入流转换为字符串,跳过message body reader
String output = org.apache.commons.io.IOUtils.toString(response.getEntityInputStream(), StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(output);

(需要确保引入了commons-io依赖,如果用这个方法的话)


内容的提问来源于stack exchange,提问作者raffa78es

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:03:19