使用xmlmapper时发生无法识别的属性异常

ymdaylpp  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(384)

xml字符串
profilesbatch包含1个profileslist
配置文件列表包含一个或多个配置文件
配置文件包含1个用户限制

<profilesBatch version="1.0" id="745" date="2012-12-13" transportKeyLabel="str1234">
  <profilesList>
    <profile iccid="str1234" profileType="str1234">
      <useRestrictions>
        <maximumDownloadAttempts>745</maximumDownloadAttempts>
        <maximumConfirmationAttempts>745</maximumConfirmationAttempts>
        <maximumNumberEIDs>745</maximumNumberEIDs>
        <maximumDownloadsPerEID>745</maximumDownloadsPerEID>
        <allowDownloadForDownloaded>true</allowDownloadForDownloaded>
        <allowDownloadForInstalled>true</allowDownloadForInstalled>
        <allowDownloadForError>true</allowDownloadForError>
      </useRestrictions>
    </profile>
  </profilesList>
</profilesBatch>

类配置文件批处理

@Getter
@Setter
@XmlRootElement(name = "ProfilesBatch")
public class ProfilesBatch
{
    @JacksonXmlProperty(isAttribute = true)
    private String date;

    @JacksonXmlElementWrapper(localName="profilesList")
    @JacksonXmlProperty(localName="profilesList")
    private ProfilesList profilesList;

    @JacksonXmlProperty(isAttribute = true)
    private String transportKeyLabel;

    @JacksonXmlProperty(isAttribute = true)
    private Header header;

    @JacksonXmlProperty(isAttribute = true)
    private String id;

    @JacksonXmlProperty(isAttribute = true)
    private String version;

}

类配置文件列表

@Getter
@Setter
@XmlRootElement(name = "ProfilesList")
public class ProfilesList
{
    @JacksonXmlElementWrapper(localName="profile")
    @JacksonXmlProperty(localName="profile")
    private List<Profile> profile;
}

班级简介

@Getter
@Setter
@XmlRootElement(name = "Profile")
public class Profile
{
    @JacksonXmlProperty(isAttribute = true)
    private String iccid;

    @JacksonXmlProperty(isAttribute = true)
    private String profileType;

    @JacksonXmlElementWrapper(localName="useRestrictions")
    private UseRestrictions useRestrictions;

}

类用户限制

@Getter
@Setter
@XmlRootElement(name = "useRestrictions")
public class UseRestrictions {

    @JacksonXmlProperty(localName = "maximumDownloadAttempts")
    private int maximumDownloadAttempts;

    @JacksonXmlProperty(localName = "maximumNumberEIDs")
    private int maximumNumberEIDs;

    @JacksonXmlProperty(localName = "maximumConfirmationAttempts")
    private int maximumConfirmationAttempts;

    @JacksonXmlProperty(localName = "maximumDownloadsPerEID")
    private int maximumDownloadsPerEID;

    @JacksonXmlProperty(localName = "allowDownloadForInstalled")
    private boolean allowDownloadForInstalled;

    @JacksonXmlProperty(localName = "allowDownloadForDownloaded")
    private boolean allowDownloadForDownloaded;

    @JacksonXmlProperty(localName = "allowDownloadForError")
    private boolean allowDownloadForError;

}

我使用的是带有jackson数据格式xml的springboot。什么时候做

XmlMapper xmlMapper = new XmlMapper();
ProfilesBatch profilesBatch = xmlMapper.readValue(data, ProfilesBatch.class);

其中data是xml字符串
我越来越

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:Unrecognized field "maximumDownloadAttempts" (class sm.movasim.sftp.service.espgmodel.Profile), not marked as ignorable

类和注解有什么问题吗?注意,所有类都有setter和getter

lb3vh1jj

lb3vh1jj1#

您实际上不需要profilelist类。
让profilebatch类中的列表直接包含以下注解:

@XmlRootElement(name = "ProfilesBatch")
public class ProfilesBatch
{
    @JacksonXmlElementWrapper(localName="profilesList")
    @JacksonXmlProperty(localName="profile")
    private List<Profile> profilesList;

另外,另一方面,在profile类中,可以对userestrictions进行注解

@JacksonXmlProperty(localName="useRestrictions")
private UseRestrictions useRestrictions;

对象不需要 Package 器注解。

eaf3rand

eaf3rand2#

修改您的 ProfilesList

@Getter
    @Setter
    @XmlRootElement(name = "ProfilesList")
    public static class ProfilesList {
        @JacksonXmlElementWrapper(localName = "profile", useWrapping = false)
        @JacksonXmlProperty(localName = "profile")
        private List<Profile> profile;
    }

原因:违约 @JacksonXmlElementWrapper 需要 Package 器标记=>您的 <profile> 清单需要另加 Package <profile></profile> 液位,设置 useWrapping = false 为了避免它。

相关问题