java-异常“找不到适合类型[简单类型…的合适构造函数”.

我需要使用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.添加不同的构造函数没有任何效果

最佳答案

R和AttSearch应该是静态的:

 

 

 public static class R {
   // other stuff

 public static class AttSearch {
   // other stuff

否则,编译器将使用外部类引用作为参数来创建默认构造函数,因此fastxml无法在没有参数的情况下找到构造函数并创建pojo.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值