java - 通过 fasterxml 将 XML 文件解析为 POJO 时出现异常 "No suitable constructor found for type [simple type..."

标签 java xml fasterxml

我需要使用 jackson-dataformat-xml 将一些 XML 文件反序列化为常规 java 对象。所以我在做:

import com.fasterxml.jackson.dataformat.xml.XmlMapper;

XmlMapper mapper = new XmlMapper();
return mapper.readValue(xmlString, Certificate.class);

xmlString 出现:

    <?xml version="1.0" encoding="UTF-8"?>
    <doc>
        <r  key="0">
            <ATT_SEARCH DM="dm1" DS="ds1" DocType="1"/>
            <ATT_SEARCH DM="dm2" DS="ds2" DocType="2"/>
        </r>
    </doc>

和类证书:

package ua.max;

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 javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import java.util.List;


@JacksonXmlRootElement(localName = "doc")
@XmlAccessorType(XmlAccessType.FIELD)
public class Certificate {

    @JacksonXmlProperty(localName = "r")
    private R r;

    public R getR() {
        return r;
    }

    public void setR(R r) {
        this.r = r;
    }


    public class R {

        @JacksonXmlProperty(localName = "ATT_SEARCH")
        @JacksonXmlElementWrapper(useWrapping = false)
        private List<AttSearch> attSearch;

        public List<AttSearch> getAttSearch() {
            return attSearch;
        }

        public void setAttSearch(List<AttSearch> attSearch) {
            this.attSearch = attSearch;
        }

        @JacksonXmlProperty(isAttribute = true, localName = "key")
        private String key;

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }


        public class AttSearch {

            @JacksonXmlProperty(isAttribute = true, localName = "DM")
            private String dm;

            @JacksonXmlProperty(isAttribute = true, localName = "DS")
            private String ds;

            @JacksonXmlProperty(isAttribute = true, localName = "DocType")
            private String docType;


            public String getDm() {
                return dm;
            }

            public void setDm(String dm) {
                this.dm = dm;
            }

            public String getDs() {
                return ds;
            }

            public void setDs(String ds) {
                this.ds = ds;
            }

            public String getDocType() {
                return docType;
            }

            public void setDocType(String docType) {
                this.docType = docType;
            }


        }


    }


}

在尝试反序列化 XML 后,我得到了异常:
“没有为类型 [简单类型,类 ua.max.Certificate$R] 找到合适的构造函数:无法从 JSON 对象实例化”

我的尝试:
1. 如果我为我的内部类添加修饰符“static”它正在工作,我得到 java 对象,但除了 2 个对象列表“ATT-SEARCH”我得到的第一个是 null
2. 加入不同的构造函数没有起到任何作用

最佳答案

RAttSearch 应该是静态的:

 public static class R {
   // other stuff

 public static class AttSearch {
   // other stuff

否则编译器会创建以外部类引用为参数的默认构造函数,因此 fasterxml 无法找到没有参数的构造函数并创建 pojo。

关于java - 通过 fasterxml 将 XML 文件解析为 POJO 时出现异常 "No suitable constructor found for type [simple type...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18374998/

相关文章:

java - 从 String 中删除空格,然后转换为 XML

java - 如何跳过解析不同格式的行。 Fasterxml csv 解析

java - 如何在 Ubuntu 上使用 Open JDK 1.9 在 ubuntu 上安装 sbt 0.13.8 或最新版本

java - 两个 <h :commandButton> in a <h:form>, 一个有效,一个无效

C# Xml 到 Linq 删除一个项目

xml - 使用 XDocument.Load 的多线程

java - 使用 FasterXML Jackson 进行奇怪的 JSON 序列化

java - 使用 com.fasterxml.jackson.databind.ObjectMapper 时,在序列化/反序列化 JSON 内容期间从异常日志中删除敏感数据

java - 缓冲输入流标记读取限制

java - DozerConverter 提示其中一个类中的 equals 方法