Search code examples
javaxmljava-17jackson-dataformat-xml

XmlMapper cannot read an XML content


I am trying to read XML content but it gives me the following error:

Unrecognized field "Blob" (class com.example.blobstorage.dto.Blobs), not marked as ignorable (one known property: "list"])

The XML is:

<?xml version="1.0" encoding="utf-8"?>
<EnumerationResults ServiceEndpoint="https://test.blob.core.windows.net/" ContainerName="publicfiles">
    <Blobs>
        <Blob>
            <Name>mainfolder/test_files/test1.pdf</Name>
            <Properties>
                <Creation-Time>Wed, 28 Feb 2024 09:28:38 GMT</Creation-Time>
                <Last-Modified>Wed, 28 Feb 2024 09:28:38 GMT</Last-Modified>
                <Etag>0x8DC383FA5196A4E</Etag>
                <Content-Length>187394</Content-Length>
                <Content-Type>application/pdf</Content-Type>
                <Content-Encoding />
                <Content-Language />
                <Content-CRC64 />
                <Content-MD5>HQWmDd6EBngY05LPdwQv9A==</Content-MD5>
                <Cache-Control />
                <Content-Disposition />
                <BlobType>BlockBlob</BlobType>
                <AccessTier>Hot</AccessTier>
                <AccessTierInferred>true</AccessTierInferred>
                <LeaseStatus>unlocked</LeaseStatus>
                <LeaseState>available</LeaseState>
                <ServerEncrypted>true</ServerEncrypted>
            </Properties>
            <OrMetadata />
        </Blob>
        <Blob>
            <Name>mainfolder/test_files/test2.pdf</Name>
            <Properties>
                <Creation-Time>Wed, 28 Feb 2024 09:28:38 GMT</Creation-Time>
                <Last-Modified>Wed, 28 Feb 2024 09:28:38 GMT</Last-Modified>
                <Etag>0x8DC383FA5191C39</Etag>
                <Content-Length>177902</Content-Length>
                <Content-Type>application/pdf</Content-Type>
                <Content-Encoding />
                <Content-Language />
                <Content-CRC64 />
                <Content-MD5>Z2nVrakvUEHHFuhnAk/bpg==</Content-MD5>
                <Cache-Control />
                <Content-Disposition />
                <BlobType>BlockBlob</BlobType>
                <AccessTier>Hot</AccessTier>
                <AccessTierInferred>true</AccessTierInferred>
                <LeaseStatus>unlocked</LeaseStatus>
                <LeaseState>available</LeaseState>
                <ServerEncrypted>true</ServerEncrypted>
            </Properties>
            <OrMetadata />
        </Blob>
    </Blobs>
    <NextMarker />
</EnumerationResults>

I am trying to read it with:

EnumerationResults enumerationResults = xmlMapper.readValue(s, EnumerationResults.class); // s is the XML in string format.

I have the following classes:

EnumerationResults.java

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class EnumerationResults implements Serializable {
    public Blobs Blobs;
}

Blobs.java

@Data
public class Blobs implements Serializable {
    public List<Blob> list;
}

Blob.java

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Blob implements Serializable {
    public String Name;
}

What I am missing?


Solution

  • Blobs class looks redundant.

    Have you tried something like this.

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class EnumerationResults implements Serializable {
        @JsonAlias("Blobs")
        public List<Blob> blobs;
    }
    

    Output:

    EnumerationResults(blobs=[Blob(Name=mainfolder/test_files/test1.pdf), Blob(Name=mainfolder/test_files/test2.pdf)])