使用 Jackson XmlMapper 将 XML 反序列化为 POJO
Posted
技术标签:
【中文标题】使用 Jackson XmlMapper 将 XML 反序列化为 POJO【英文标题】:XML deserialization to POJO using Jackson XmlMapper 【发布时间】:2014-10-22 18:39:50 【问题描述】:使用 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)
【问题讨论】:
【参考方案1】:您的POJO
课程不适合您的XML
。结构比你想象的要简单。见下例:
@JacksonXmlRootElement(localName = "open")
class OpenCredentials
@JacksonXmlProperty(localName = "creds")
@JacksonXmlElementWrapper(useWrapping = false)
private Credentials[] credentials;
//getters, setters, toString
class Credentials
@JacksonXmlProperty(isAttribute = true)
private String priv;
@JacksonXmlProperty(isAttribute = true)
private String type;
private String user;
@JacksonXmlProperty(localName = "client_token")
private String clientToken;
@JacksonXmlProperty(localName = "client_secret")
private String clientSecret;
//getters, setters, toString
简单用法:
XmlMapper mapper = new XmlMapper();
OpenCredentials openCredentials = mapper.readValue(XML, OpenCredentials.class);
System.out.println(openCredentials);
上面的程序打印(为您的XML
):
OpenCredentialscredentials=[Credentialspriv='write', type='internal', user='Username1', client_token='abcplaudzrbcy37c', client_secret='0cxDE3LE0000=', Credentialspriv='read', type='internal', user='Username1', client_token='123plaudzrbcy37c', client_secret='0cxDE3LE1234=', Credentialspriv='none', type='internal', user='Username1', client_token='000plaudzrbcy37c', client_secret='0cxDE3LEabcd=']
另见:
-
jackson-dataformat-xml。
Home: Jackson XML databind Wiki。
【讨论】:
我得到“无法识别的字段”将 localName 设置为 JacksonXmlElementWrapper 注释,拆分为 @MichalZiober 示例后,反序列化工作正常以上是关于使用 Jackson XmlMapper 将 XML 反序列化为 POJO的主要内容,如果未能解决你的问题,请参考以下文章
使用 Jackson XmlMapper 序列化为 XML DOM