javajackson-dataformat-xml

I can't deserialize a wrapped List with Jackson XmlMapper


I'm trying to deserialize an XML list returned from a SOAP request, but I'm getting the following error:

com.fasterxml.jackson.databind.JsonMappingException: Duplicate property 'LstMyClassInfo' for [simple type, class MyClassBLL] at [Source: (StringReader); line: 1, column: 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);
    }

This is XML sample to deserialize:

<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>

Anybody help-me?

PS: sorry for my bad english

I've already checked the JacksonXml documentation, but there doesn't seem to be anything wrong with my implementation.


Solution

  • You would need to add @JacksonXmlProperty to the fields in the MyClassBLL class that matches with the xml element names.

    Example

    @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;
    }