使用Jackson进行反序列化Xml到列表失败

时间:2019-05-11 06:10:57

标签: java xml list class jackson

我正在尝试反序列化我的Xml文件并将其映射到我的班级,如果我没有将List附加到我的班级上,它就可以正常工作。

我已经使用XMLElementWrapper添加了静态类jsonIgnoreProperties,但仍然无法正常工作!

这是即时通讯方法。

File file = new File(test.xml);
XmlMapper xmlMapper = new XmlMapper();
String xml = inputStreamToString(new FileInputStream(file));

Test test = xmlMapper.readValue(xml, Test.class);

这是我的课程。

import java.util.List;
import org.codehaus.jackson.annotate.JsonProperty;
import com.fasterxml.jackson.xml.annotate.JacksonXmlElementWrapper;
import com.fasterxml.jackson.xml.annotate.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "web-request-form")
public class Test {

@JsonProperty("attachments")
public Attachments attachments;

public Test(Attachments attachments) {
    super();
    this.attachments = attachments;
}

public Test() {
    super();
}

public Attachments getAttachments() {
    return attachments;
}

public void setAttachments(Attachments attachments) {
    this.attachments = attachments;
}

public static class Attachments {


    @JacksonXmlElementWrapper(localName = "attachment")
    public List<Attachment> attachment;

    public Attachments() {
        super();
    }

    public Attachments(List<Attachment> attachment) {
        super();
        this.attachment = attachment;
    }

    public List<Attachment> getAttachment() {
        return attachment;
    }

    public void setAttachment(List<Attachment> attachment) {
        this.attachment = attachment;
    }
}

public static class Attachment {

    @JsonProperty("filename")
    public String fileName;

    @JsonProperty("desc")
    public String desc;

    @JsonProperty("size")
    public String size;

    public Attachment() {
        super();
    }

    public Attachment(String fileName, String desc, String size) {
        super();
        this.fileName = fileName;
        this.desc = desc;
        this.size = size;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }
}

}

这是我的xml文件。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This file was generated from an ASPX file-->
<web-request-form xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="WebRequestForm.elms.xmlbeans.novacitynets.com">
 <attachments>
  <attachment>
    <filename>test</filename>
    <desc />
    <size>2089.801</size>
  </attachment>
 </attachments>
</web-request-form>

这是我遇到的错误。

Can not instantiate value of type [simple type, class 
package.Test$Attachment] from JSON String; no single-String 
constructor/factory method (through reference chain: 
package.Test["attachments"]->package.Attachments["attachment"])

2 个答案:

答案 0 :(得分:2)

我不确定您的Jackson库版本,但是如果您使用的是2.1版或更高版本,可以在下面尝试此代码吗? 另外,混合使用codehausfasterxml似乎也不好。

尝试以下操作:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.List;

@JacksonXmlRootElement(localName = "web-request-form")
public class Test {
    @JsonProperty("attachments")
    public Attachments attachments;

    public static class Attachments {
        // @JacksonXmlElementWrapper(localName = "attachment")
        @JacksonXmlElementWrapper(useWrapping = false, localName = "attachment")
        public List<Attachment> attachment;

        // ...
    }

    public static class Attachment {
        @JsonProperty("filename")
        public String fileName;

        @JsonProperty("desc")
        public String desc;

        @JsonProperty("size")
        public String size;

        // ...
    }
}

如果您将useWrapping选项应用于JacksonXmlElementWrapper注释,则不会有任何改变。调用方法如下:

File file = new File("test.xml");

// If you use the useWrapping option globally
// JacksonXmlModule module = new JacksonXmlModule();
// module.setDefaultUseWrapper(false);

XmlMapper xmlMapper = new XmlMapper(module);
String xml = inputStreamToString(new FileInputStream(file));
Test test = xmlMapper.readValue(xml, Test.class);

答案 1 :(得分:1)

我设法通过

对其进行了解析
<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.9.8</version>
</dependency>

实体:

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

import java.util.ArrayList;
import java.util.List;

@JacksonXmlRootElement(localName = "web-request-form")
public class WebRequestForm {

    public Attachments attachments;

    public static class Attachments {

        @JacksonXmlElementWrapper(useWrapping = false)
        public List<Attachment> attachment = new ArrayList<>();

    }

    public static class Attachment {

        @JacksonXmlProperty(localName = "filename")
        public String fileName;

        public String desc;

        public String size;

    }
}

解析器:

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;

    public class WebRequestFormParser {

        public WebRequestForm parse(String sampleXMLContent) throws IOException {
            XmlMapper xmlMapper = new XmlMapper();
            WebRequestForm result = xmlMapper.readValue(sampleXMLContent, WebRequestForm.class);
            return result;
        }
    }

请参阅项目https://github.com/sbzDev/stackoverflow/tree/master/question56087719 进行单元测试