java 我无法使用JacksonXmlMapper反序列化 Package 的List

vatpfxk5  于 2023-06-04  发布在  Java
关注(0)|答案(1)|浏览(164)

我尝试反序列化从SOAP请求返回的XML列表,但出现以下错误:

  • com.fasterxml.Jackson.databind.JsonMappingException:[Source:]处的[simple type,class MyClassBLL]的重复属性“LstMyClassInfo”(String);行:1,列:1]*
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@JsonPropertyOrder({"LstMyClassInfo", "DtIni", "DtEnd", "LstNumbers"})
public class MyClassBLL {
    @JacksonXmlElementWrapper(localName = "LstMyClassInfo")
    public List<MyClassInfo> MyClassInfoList;
    public String DtIni;
    public String DtFDtEndnal;
    @JacksonXmlElementWrapper(localName = "LstNumbers")
    public List<String> Numbers;
}
private MyClassBLL deserialize(String captureInformation) throws Exception {
        JacksonXmlModule xmlModule = new JacksonXmlModule();
        xmlModule.setDefaultUseWrapper(false);
        ObjectMapper objectMapper = new XmlMapper(xmlModule);

        return objectMapper.readValue(captureInformation, MyClassBLL.class);
    }

这是要反序列化的XML示例:

<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</DtEd>
    <LstNumbers>
        <string>XXXXXXXXX</string>
        <string>YYYYYYYYY</string>
        <string>ZZZZZZZZZ</string>
    </LstNumbers>
</MyClassBLL>

有人能帮帮我吗
PS:对不起,我的英语不好
我已经检查了JacksonXml文档,但我的实现似乎没有任何问题。

tpxzln5u

tpxzln5u1#

您需要将@JacksonXmlProperty添加到MyClassBLL类中与xml元素名称匹配的字段中。
示例

@Getter
@Setter
@ToString
@JsonPropertyOrder({ "LstMyClassInfo", "DtIni", "DtEnd", "LstNumbers" })
public class MyClassBLL {

    @JacksonXmlElementWrapper(localName = "LstMyClassInfo")
    @JacksonXmlProperty(localName="MyClassInfo")
    public List<MyClassInfo> MyClassInfoList;
    public String DtIni;
    public String DtEnd;
    @JacksonXmlElementWrapper(localName = "LstNumbers")
    @JacksonXmlProperty(localName="string")
    public List<String> Numbers;
}

相关问题