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

使用Jackson XmlMapper反序列化包装List遇重复属性错误求助

解决Jackson XML反序列化时的"Duplicate property"错误

这个错误是因为Jackson XML模块解析时,同时识别到两个指向LstMyClassInfo的映射来源,导致属性重复冲突,具体原因和修复方案如下:

问题根源

  • 注解配置不完整:仅给列表字段添加@JacksonXmlElementWrapper指定外层包裹标签,但未明确指定列表内单个元素的标签名。结合全局设置xmlModule.setDefaultUseWrapper(false),Jackson会同时尝试将字段名映射为XML标签,又识别到注解指定的LstMyClassInfo标签,从而判定为重复属性。
  • 实体类字段笔误:DtFDtEndnal字段名拼写错误,与XML中的<DtEnd>不匹配,会导致该字段无法被正确赋值。
  • XML格式错误:示例XML中<DtEnd>的闭合标签写成了</DtEd>,会触发XML格式解析错误。

修复方案

1. 修正实体类注解与字段

给列表字段补充@JacksonXmlProperty指定单个元素的标签名,同时修正字段拼写:

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@JsonPropertyOrder({"LstMyClassInfo", "DtIni", "DtEnd", "LstNumbers"})
public class MyClassBLL {
    @JacksonXmlElementWrapper(localName = "LstMyClassInfo")
    @JacksonXmlProperty(localName = "MyClassInfo")
    public List<MyClassInfo> myClassInfoList; // 改用小驼峰命名,符合Java规范
    public String DtIni;
    public String DtEnd; // 修正拼写错误
    @JacksonXmlElementWrapper(localName = "LstNumbers")
    @JacksonXmlProperty(localName = "string")
    public List<String> numbers; // 改用小驼峰命名
}

2. 调整反序列化配置

移除xmlModule.setDefaultUseWrapper(false)全局设置,避免和注解的显式配置冲突:

private MyClassBLL deserialize(String captureInformation) throws Exception {
    JacksonXmlModule xmlModule = new JacksonXmlModule();
    ObjectMapper objectMapper = new XmlMapper(xmlModule);
    // 可选:忽略XML中未定义的未知属性,避免解析报错
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper.readValue(captureInformation, MyClassBLL.class);
}

3. 修正XML格式错误

将示例XML中的</DtEd>改为</DtEnd>:

<MyClassBLL xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <LstMyClassInfo>
        <MyClassInfo>
            <SNumber>XXXXXXXXX</SNumber>
            <!-- Any other properties -->
        </MyClassInfo>
        <MyClassInfo>
            <SNumber>YYYYYYYYY</SNumber>
            <!-- Any other properties -->
        </MyClassInfo>
        <MyClassInfo>
            <SNumber>ZZZZZZZZZ</SNumber>
            <!-- Any other properties -->
        </MyClassInfo>
    </LstMyClassInfo>
    <DtIni>2023-05-01T03:00:00</DtIni>
    <DtEnd>2023-06-01T02:59:59</DtEnd> <!-- 修正闭合标签 -->
    <LstNumbers>
        <string>XXXXXXXXX</string>
        <string>YYYYYYYYY</string>
        <string>ZZZZZZZZZ</string>
    </LstNumbers>
</MyClassBLL>

验证逻辑

修正后,Jackson会正确将<LstMyClassInfo>识别为列表的外层包裹标签,内部<MyClassInfo>元素映射到myClassInfoList列表;同理<LstNumbers>包裹的<string>元素会映射到numbers列表,不会再出现重复属性冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 23:35:29