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

Maven中排除嵌套依赖:解决Gson版本冲突引发的方法缺失错误

问题分析

这个错误的核心是Gson版本冲突:JsonParser.parseString()是Gson 2.8.0及以上版本新增的方法,而你的项目中实际加载的是integrator.jar依赖的旧版本Gson,导致找不到该方法。你之前的排除配置无效,是因为Gson是payment-connector的间接嵌套依赖,直接排除payment-connector的Gson无法触达深层的integrator.jar依赖。

解决方案

方法1:通过Maven Dependency Management强制统一Gson版本

这是最简洁有效的方案,在payment-orchestrator的pom.xml中添加dependencyManagement节点,强制整个依赖树使用你指定的Gson版本,无视任何嵌套依赖的版本声明:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${version.gson}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

保留你原来的依赖配置即可,无需额外添加exclusion,Maven会自动将所有Gson依赖的版本统一为你指定的${version.gson}。

方法2:定位依赖树并深度排除旧版本Gson

如果方法1无法满足需求,先执行Maven命令生成完整依赖树,找到旧版本Gson的具体依赖路径:

mvn dependency:tree

在输出中找到类似如下的层级(示例):

com.payment:connector:jar:1.0.1
  \- com.payment:integrator:jar:x.x.x
     \- com.google.code.gson:gson:jar:2.6.2

接下来,在payment-orchestrator的pom.xml中,先排除payment-connector对integrator的依赖,再单独引入integrator并排除其Gson依赖:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>${version.gson}</version>
</dependency>
<!-- 原connector依赖,排除integrator -->
<dependency>
    <groupId>com.payment</groupId>
    <artifactId>connector</artifactId>
    <version>1.0.1</version>
    <exclusions>
        <exclusion>
            <groupId>com.payment</groupId>
            <artifactId>integrator</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- 单独引入integrator,排除其Gson依赖 -->
<dependency>
    <groupId>com.payment</groupId>
    <artifactId>integrator</artifactId>
    <version>x.x.x</version> <!-- 替换为依赖树中显示的实际版本 -->
    <exclusions>
        <exclusion>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </exclusion>
    </exclusions>
</dependency>

方法3:修改payment-connector项目(若有权限)

如果你能维护payment-connector项目,直接在其pom.xml中添加dependencyManagement锁定Gson版本,或者在integrator的依赖中排除Gson,重新打包发布新版本后,payment-orchestrator引入时就不会出现版本冲突。


内容的提问来源于stack exchange,提问作者S.Balaban

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 15:55:28