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

XmlMapper读取XML遇Unrecognized field 'Blob'错误,求解决方法

问题:XmlMapper解析XML时出现Unrecognized field错误

错误信息

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

待解析的XML内容

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

解析代码

EnumerationResults enumerationResults = xmlMapper.readValue(s, EnumerationResults.class); // s为XML字符串

对应的Java实体类

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

问题原因与解决方法

原因

XmlMapper默认按实体类字段名匹配XML标签名,但Blobs类中的list字段和XML里的<Blob>标签名称不匹配,导致框架无法识别XML中的<Blob>元素,抛出Unrecognized field错误。

解决方法

使用Jackson的XML注解,将Blobs类的list字段与XML中的<Blob>标签做映射,修改Blobs.java如下:

@Data
public class Blobs implements Serializable {
    // 禁用外层包装,直接将多个<Blob>标签映射为List集合
    @JacksonXmlElementWrapper(useWrapping = false)
    // 指定XML元素名为Blob
    @JacksonXmlProperty(localName = "Blob")
    public List<Blob> list;
}

补充说明

  • 确保项目已引入jackson-dataformat-xml依赖,否则上述XML注解无法生效。
  • 若不想修改字段名逻辑,也可将list字段改名为blob,但仍需配合@JacksonXmlProperty(localName = "Blob")指定XML标签名,避免大小写匹配问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 09:40:55