微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Jackson XmlMapper将XML反序列化为POJO

如何解决使用Jackson XmlMapper将XML反序列化为POJO

您的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):

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='}]}

也可以看看:

  1. jackson-dataformat-xml
  2. 主页:Jackson XML databind Wiki

解决方法

使用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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。