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

Apache Commons Configuration2加载JSON配置文件失败求助

Commons Configuration2 无法加载JSON配置文件的解决方法

问题根源

你在XML配置中使用<properties>标签加载JSON文件,但该标签仅对应PropertiesConfiguration,只能解析传统的.properties键值对格式,无法识别JSON结构,导致JSON配置未被正确加载。

解决方案

1. 修改XML配置文件

将加载JSON文件的<properties>标签替换为专门的<json>标签:

<configuration>
  <properties fileName="this-works.properties" />
  <json fileName="this-does-not-work.json" />
</configuration>

<json>标签会初始化JsonConfiguration实例,专门用于解析JSON格式的配置文件,能正确识别{"foo": "bar"}这样的键值结构。

2. 确保依赖齐全

Commons Configuration2的JSON解析依赖Jackson库,需要确认项目中包含以下依赖(以Maven为例):

<!-- Commons Configuration2 核心 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-configuration2</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- Commons Configuration2 JSON扩展模块 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-configuration2-jackson</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- Jackson 核心解析库 -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.4.2</version> <!-- 选择与Configuration2兼容的版本即可 -->
</dependency>

3. 验证配置加载

修改后重新运行测试代码,configuration.getString("foo")应该能正确返回"bar",测试通过。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 04:01:14