XML 反序列化为 POJO 使用 Jackson XmlMapper
2022-09-01 18:40:14
使用 Jackson XmlMapper 注释,我如何将这个 XML 反序列化为 pojo?
<?xml version="1.0" encoding="UTF-8"?>
<open>
<creds priv="write" type="internal">
<user>Username1</user>
<client_token>abcplaudzrbcy37c</client_token>
<client_secret>0cxDE3LE0000=</client_secret>
</creds>
<creds priv="read" type="internal">
<user>Username1</user>
<client_token>123plaudzrbcy37c</client_token>
<client_secret>0cxDE3LE1234=</client_secret>
</creds>
<creds priv="none" type="internal">
<user>Username1</user>
<client_token>000plaudzrbcy37c</client_token>
<client_secret>0cxDE3LEabcd=</client_secret>
</creds>
</open>
我试图使用这样的东西:
@JacksonXmlRootElement(localName = "Open")
public class OpenCredentials {
@JacksonXmlProperty(localName = "Credentials")
private Credentials[] credentials;
}
class Credentials {
@JacksonXmlProperty(isAttribute = true)
private String priv;
@JacksonXmlProperty(isAttribute = true)
private String type;
@JacksonXmlProperty(localName = "Creds")
private Creds[] creds;
}
class Creds {
@JacksonXmlText(value = true)
private String user;
@JacksonXmlText(value = true)
private String client_token;
@JacksonXmlText(value = true)
private String client_secret;
}
当我尝试使用XmlMapper的readValue()时,我得到以下错误:
com.fasterxml.jackson.databind.JsonMappingException: Duplicate property '' for [simple type, class com.company.data.utils.api.Creds]
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268)
at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:243)