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

引入graphql-java-client-runtime后spring-boot:run读取classpath JSON报错

Spring Boot mvn spring-boot:run 触发zip END header not found问题(关联graphql-java-client-runtime依赖)

问题复现

  • Spring Boot应用需通过classpath*:/*.json读取类路径下的JSON文件
  • Eclipse内启动、mvn clean package后执行java -jar ...启动均正常
  • 执行mvn clean spring-boot:run时抛出java.util.zip.ZipException: zip END header not found错误
  • 排查确认:错误由com.graphql-java-generator:graphql-java-client-runtime:2.8依赖引发;排除该依赖下的graphql-java-client-dependencies和graphql-java-common-runtime可消除报错,但会导致应用缺失必要依赖无法运行;改用@Value("classpath*:/*.json")注入资源也会触发相同错误

原因分析

该错误本质是资源解析器处理classpath*模式时,误将非Zip/Jar格式的资源当作压缩包解析,或类路径中存在损坏的依赖包。graphql-java-client-runtime:2.8的子依赖在spring-boot:run的类路径构建流程中,可能修改了类加载逻辑或引入了异常资源,导致Spring的资源扫描逻辑出错。

解决方案

1. 缩小资源扫描范围

避免使用classpath*:/*.json这种根路径全量扫描,改为指定具体目录,例如JSON文件放在src/main/resources/config下的话,使用classpath*:config/*.json。缩小扫描范围可避开依赖带来的解析逻辑冲突。

2. 调整spring-boot:run的类加载器配置

在pom.xml中配置spring-boot-maven-plugin使用系统类加载器,绕过依赖引发的类加载问题:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <useSystemClassloader>true</useSystemClassloader>
            </configuration>
        </plugin>
    </plugins>
</build>

3. 升级依赖版本

graphql-java-client-runtime:2.8可能存在已知bug,尝试升级至更高稳定版本(如3.x系列),新版本通常会修复类路径资源解析相关问题,同时保留必要的功能依赖。

4. 自定义资源读取逻辑

绕过Spring默认的classpath*解析逻辑,手动遍历类路径资源:

import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.net.URL;

public List<ClassPathResource> getJsonResources() throws IOException {
    List<ClassPathResource> jsonResources = new ArrayList<>();
    ClassLoader classLoader = getClass().getClassLoader();
    // 遍历类路径根目录下的资源
    Enumeration<URL> resources = classLoader.getResources("");
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        String path = url.getPath();
        if (path.endsWith(".json")) {
            // 截取相对路径,避免绝对路径问题
            String relativePath = path.substring(path.lastIndexOf("classes/") + 8);
            jsonResources.add(new ClassPathResource(relativePath));
        }
    }
    return jsonResources;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 23:48:25