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

Jackson升级至2.14.2后JavaPropsMapper.readValue方法兼容问题求助

解决Jackson 2.14.2中JavaPropsMapper无法直接读取Properties的问题

问题原因

Jackson从2.9版本开始,移除了JavaPropsMapper类中接收java.util.Properties作为输入的readValue重载方法,底层输入处理逻辑调整为优先支持流(InputStream/Reader)类输入。

替代实现方案

方案1:使用官方新增的readPropertiesAs方法(推荐)

Jackson 2.9+专门添加了readPropertiesAs方法来替代原来的readValue(Properties, Class),逻辑完全一致,能直接将Properties解析为目标类型:

// javaPropsMapper是com.fasterxml.jackson.dataformat.javaprop.JavaPropsMapper实例
Properties prop = new Properties();
prop.setProperty("a.key", "value");

JsonNode result = javaPropsMapper.readPropertiesAs(prop, JsonNode.class);

执行后会生成和旧版本一致的JsonNode:{"a":{"key":"value"}}

方案2:将Properties转为流输入

如果需要兼容更灵活的输入场景,可以把Properties对象转为字节流/字符流,再传递给readValue方法:

Properties prop = new Properties();
prop.setProperty("a.key", "value");

// 将Properties内容写入字节流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
prop.store(baos, null); // 第二个参数为注释,可传null

// 转为字节输入流后读取
JsonNode result = javaPropsMapper.readValue(new ByteArrayInputStream(baos.toByteArray()), JsonNode.class);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 18:22:15