java - new - xmlmapper jackson
Deserialización de XML a POJO usando Jackson XmlMapper (1)
Sus clases POJO
no se ajustan a su XML
. La estructura es más simple de lo que creías Vea el siguiente ejemplo:
@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
}
Uso simple:
XmlMapper mapper = new XmlMapper();
OpenCredentials openCredentials = mapper.readValue(XML, OpenCredentials.class);
System.out.println(openCredentials);
Sobre las impresiones del programa (para su 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=''}]}
Ver también:
Usando las anotaciones de Jackson XmlMapper, ¿cómo podría deserializar este XML en un 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>
Intenté usar algo como esto:
@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;
}
Cuando traté de usar readValue () de XmlMapper, recibo el siguiente 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)