杰克逊XML无法在数组内部使用数组

时间:2019-07-05 05:24:07

标签: java xml jackson jackson-dataformat-xml

我正在使用Jackson将XML反序列化为POJO:

<ezCertApplication>
    <AccountRef>...</AccountRef>
    <AppID>...</AppID>
    ...
    <items>
        <Item>
            <CertificateReference>...</CertificateReference>
            ...
        </Item>
    </items>
</ezCertApplication>
<ezCertApplication>
    <AccountRef>...</AccountRef>
    <AppID>...</AppID>
    ...
    <items>
        <Item>
            <CertificateReference>...</CertificateReference>
            ...
        </Item>
    </items>
</ezCertApplication>

这些类如下:

public class ArrayOfezCertApplication
{
    @JacksonXmlElementWrapper(useWrapping = false)    
    @JacksonXmlProperty(localName="ezCertApplication")
    private List<EzCertApplication> applications;

    public Collection<EzCertApplication> getApplications()
    {
        return applications;
    }    
}


@JsonIgnoreProperties(ignoreUnknown = true)
public class EzCertApplication
{
    @JacksonXmlProperty(localName="AccountRef")
    private String accountRef;

    @JacksonXmlProperty(localName="ApplicationRef")
    private String applicationRef;

    ...

    @JacksonXmlProperty(localName="items")
    private List<Item> items;
}


@JsonIgnoreProperties(ignoreUnknown = true)
public class Item
{
    @JacksonXmlProperty(localName="CertificateReference")
    private String certificateReference;


    public String getCertificateReference()
    {
        return certificateReference;
    }
}

我正在这样反序列化:

new XmlMapper().readValue(src, ArrayOfezCertApplication.class);

运行此命令时,项目会反序列化,但我只能从数组中获取最后一个ezCertApplication。如果我更改为:

@JacksonXmlElementWrapper(useWrapping = false)    
@JacksonXmlProperty(localName="items")
private List<Item> items;

然后,我从数组中获取了所有的ezCertApplications,但是如果每个数组的项均为null,则该数组。

我认为这是因为XML中存在两种不同类型的数组,我无法控制它们。

我如何才能使其正确反序列化?

0 个答案:

没有答案