使用 XmlMapper 时发生 UnrecognizedPropertyException
UnrecognizedPropertyException when using XmlMapper
XML 字符串
一个 profilesBatch 包含 1 个 profilesList
profilesList 包含一个或多个配置文件
配置文件包含 1 个 useRestrictions
<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>
Class 配置文件批处理
@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;
}
Class 配置文件列表
@Getter
@Setter
@XmlRootElement(name = "ProfilesList")
public class ProfilesList
{
@JacksonXmlElementWrapper(localName="profile")
@JacksonXmlProperty(localName="profile")
private List<Profile> profile;
}
Class个人资料
@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;
}
Class 使用限制
@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;
}
我正在使用 spring-boot with jackson-dataformat-xml。做的时候
XmlMapper xmlMapper = new XmlMapper();
ProfilesBatch profilesBatch = xmlMapper.readValue(data, ProfilesBatch.class);
其中数据是 xml 字符串
正在
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:Unrecognized field "maximumDownloadAttempts" (class sm.movasim.sftp.service.espgmodel.Profile), not marked as ignorable
类和注解有什么问题吗?请注意,所有 类 都有 setter 和 getter
修改你的ProfilesList
class
@Getter
@Setter
@XmlRootElement(name = "ProfilesList")
public static class ProfilesList {
@JacksonXmlElementWrapper(localName = "profile", useWrapping = false)
@JacksonXmlProperty(localName = "profile")
private List<Profile> profile;
}
原因:默认 @JacksonXmlElementWrapper
需要包装器标签 => 您的 <profile>
列表需要包装在另一个 <profile></profile>
级别,请设置 useWrapping = false
来避免它。
您实际上并不需要 ProfileList class。
直接在 ProfileBatch class 中添加这些注释的列表:
@XmlRootElement(name = "ProfilesBatch")
public class ProfilesBatch
{
@JacksonXmlElementWrapper(localName="profilesList")
@JacksonXmlProperty(localName="profile")
private List<Profile> profilesList;
另外,在旁注中,在配置文件中 class UseRestrictions 可以被注释
@JacksonXmlProperty(localName="useRestrictions")
private UseRestrictions useRestrictions;
您不需要对象的包装器注释。
XML 字符串
一个 profilesBatch 包含 1 个 profilesList
profilesList 包含一个或多个配置文件
配置文件包含 1 个 useRestrictions
<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>
Class 配置文件批处理
@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;
}
Class 配置文件列表
@Getter
@Setter
@XmlRootElement(name = "ProfilesList")
public class ProfilesList
{
@JacksonXmlElementWrapper(localName="profile")
@JacksonXmlProperty(localName="profile")
private List<Profile> profile;
}
Class个人资料
@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;
}
Class 使用限制
@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;
}
我正在使用 spring-boot with jackson-dataformat-xml。做的时候
XmlMapper xmlMapper = new XmlMapper();
ProfilesBatch profilesBatch = xmlMapper.readValue(data, ProfilesBatch.class);
其中数据是 xml 字符串
正在
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:Unrecognized field "maximumDownloadAttempts" (class sm.movasim.sftp.service.espgmodel.Profile), not marked as ignorable
类和注解有什么问题吗?请注意,所有 类 都有 setter 和 getter
修改你的ProfilesList
class
@Getter
@Setter
@XmlRootElement(name = "ProfilesList")
public static class ProfilesList {
@JacksonXmlElementWrapper(localName = "profile", useWrapping = false)
@JacksonXmlProperty(localName = "profile")
private List<Profile> profile;
}
原因:默认 @JacksonXmlElementWrapper
需要包装器标签 => 您的 <profile>
列表需要包装在另一个 <profile></profile>
级别,请设置 useWrapping = false
来避免它。
您实际上并不需要 ProfileList class。
直接在 ProfileBatch class 中添加这些注释的列表:
@XmlRootElement(name = "ProfilesBatch")
public class ProfilesBatch
{
@JacksonXmlElementWrapper(localName="profilesList")
@JacksonXmlProperty(localName="profile")
private List<Profile> profilesList;
另外,在旁注中,在配置文件中 class UseRestrictions 可以被注释
@JacksonXmlProperty(localName="useRestrictions")
private UseRestrictions useRestrictions;
您不需要对象的包装器注释。