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

Unirest无法识别pom.xml中Gson mapper依赖问题咨询

解决Unirest无法识别unirest-object-mappers-gson依赖的问题

核心原因

你遇到的问题主要是版本不匹配或依赖artifactId变更导致的:

  • 使用LATEST作为版本号时,Maven可能拉取到unirest-java和JSON映射库的不同大版本,两者兼容性差,无法加载JSON解析实现。
  • Unirest 3.x及以上版本中,GSON映射器的artifactId已从unirest-object-mappers-gson改为unirest-gson,若主库是高版本但依赖仍用旧artifactId,自然无法识别。

解决方案

方案1:使用匹配的稳定版本(推荐)

放弃LATEST,给unirest-java和GSON映射器指定相同的稳定版本(以当前最新稳定版4.0.0为例):

<dependencies>
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-java</artifactId>
        <version>4.0.0</version>
    </dependency>

    <!-- Unirest 3.x+版本对应的GSON映射器 -->
    <dependency>
        <groupId>com.konghq</groupId>
        <artifactId>unirest-gson</artifactId>
        <version>4.0.0</version>
    </dependency>

    <!-- 其他依赖可同步替换为具体稳定版本 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.23.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.23.1</version>
    </dependency>

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.10.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>4.1</version>
    </dependency>
</dependencies>

方案2:适配Unirest 2.x旧版本

如果你的unirest-java是2.x版本,保留原artifactId,但必须指定匹配的版本号:

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>2.4.03</version>
</dependency>

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-object-mappers-gson</artifactId>
    <version>2.4.03</version>
</dependency>

额外验证步骤

  • 执行mvn clean install清理并重新构建项目,确保依赖被正确下载。
  • 运行mvn dependency:tree查看依赖树,确认GSON映射器已被成功引入,无版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 20:30:31