使用Jackson XmlMapper对POJO进行XML反序列化 [英] XML deserialization to POJO using Jackson XmlMapper
本文介绍了使用Jackson XmlMapper对POJO进行XML反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Jackson XmlMapper注释,如何将此XML反序列化为pojo?
Using Jackson XmlMapper annotations, how would I deserialize this XML into a 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>
我试图使用这样的东西:
I attempted to use something like this:
@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()时,出现以下错误:
When I tried to use XmlMapper's readValue(), I get the following error:
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)
推荐答案
您的 POJO
类不适合您的 XML
。结构比你想象的简单。见下面的例子:
Your POJO
classes do not fit to your XML
. The structure is simpler than you thought. See below example:
@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
) :
Above program prints (for your XML
):
OpenCredentials{credentials=[Credentials{priv='write', type='internal', user='Username1', client_token='abcplaudzrbcy37c', client_secret='0cxDE3LE0000='}, Credentials{priv='read', type='internal', user='Username1', client_token='123plaudzrbcy37c', client_secret='0cxDE3LE1234='}, Credentials{priv='none', type='internal', user='Username1', client_token='000plaudzrbcy37c', client_secret='0cxDE3LEabcd='}]}
参见:
- jackson-dataformat-xml.
- Home: Jackson XML databind Wiki.
这篇关于使用Jackson XmlMapper对POJO进行XML反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文