ジャクソンを使用してリストにDeserialise XMLが失敗しました

ロス:

私は私のクラスにリストを添付していなかった場合、私のXMLファイルをdeserialise、私のクラスにマップしようとしているイムは、それが正常に動作しています。

XMLElementWrapperを使用してすでに追加静的クラス、jsonIgnorePropertiesを、IVEが、そのはまだ働いていません!

これは、メソッドイム呼び出しです。

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"])
madplayの:

私は確かにあなたのジャクソンのライブラリバージョンのないんだけど、あなたは、バージョン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);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=234606&siteId=1