Search code examples
javaxmljacksonxml-deserializationfasterxml

Jackson XMLWrapper failing to deserialize this xml into pojo?


Consider the given xml below:

<?xml version="1.0"?>
<Records>
    <Record>
        <Prop name="commerce">sku1</Prop>
        <Prop name="commerce">sku2</Prop>
        <Prop name="commerce">sku3</Prop>
        <Prop name="commerce">sku4</Prop>
    </Record>
    <Record>
        <Prop name="commerce">sku10</Prop>
        <Prop name="commerce">sku20</Prop>
        <Prop name="commerce">sku30</Prop>
        <Prop name="commerce">sku40</Prop>
    </Record>
</Records>

Now consider these Pojos that I have created(removing getter and setters for brevity).

public class Records {
    private List<Record> records;
}

public class Record {
    private List<Prop> props;
}

public class Prop {
    // I want to capture name attribute
    private String name;
    // But I also want to capture what comes inside Prop element as well. This would then have values like sku1, sku2 etc
    private String text;
}

Now I have code using XMLWrapper like this.

File file = ResourceUtils.getFile("classpath:sample.xml"); // the above XML is sample.xml
XmlMapper xmlMapper = XmlMapper.builder().build();
Records records = xmlMapper.readValue(file, Records.class); // this line is failing

But I am getting an error like this

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Record" (class com.example.demo.model.xml.Records), not marked as ignorable (one known property: "records"])

Solution

  • You have to combine the @JacksonXmlProperty annotation with the @JacksonXmlElementWrapper annotation that indicates the wrapper element to use with a Collection: in this case the optional property useWrapping is set to false to explicitly disable the wrapping:

    public class Records {
        @JacksonXmlProperty(localName = "Record")
        @JacksonXmlElementWrapper(useWrapping = false)
        private List<Record> records;
    }
    
    public class Record {
        @JacksonXmlProperty(localName = "Prop")
        @JacksonXmlElementWrapper(useWrapping = false)
        private List<Prop> props;
    }
    
    public class Prop {
        //<Prop name="commerce">sku1</Prop> name is an attribute with value commerce
        @JacksonXmlProperty(localName= "name", isAttribute = true)
        private String name;
        
        //<Prop name="commerce">sku1</Prop> sku1 is the text inside the Prop tags
        @JacksonXmlText
        private String text;
    }