没有从字符串值'E1'反序列化的字符串参数构造函数/工厂方法

问题描述

无法将 xml 转换为 java pojo 对象。 请查看以下详细信息:-

输入xml-

<?xml version='1.0' encoding='UTF-8'?>
<Company>
<Employee>
<Name>E1</Name>
<Id>123</Id>
</Employee>
<Employee>
<Name>E2</Name>
<Id>678</Id>
</Employee>
</Company>

pom.xml 有依赖:-

<dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.9.6</version>
</dependency>

公司.Java

package com.test;
import java.util.List;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement
public class Company {
    
    @JacksonXmlProperty(localName = "Employee")
    private List<Employee> employee;
    
    public Company() {}

    public Company(List<Employee> employee) {
        super();
        this.employee = employee;
    }

    public List<Employee> getEmployee() {
        return employee;
    }

    public void setEmployee(List<Employee> employee) {
        this.employee = employee;
    }

    
}

Employee.java

package com.test;

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

public class Employee {
    
    @JacksonXmlProperty(localName = "Name")
    private String name;
    
    @JacksonXmlProperty(localName = "Id")
    private String id;
    
    public Employee() {}

    public Employee(String name,String id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    

}

将xml转换为java代码:-

JacksonXmlModule module = new JacksonXmlModule();
XmlMapper objectMapper = new XmlMapper(module);
module.setDefaultUseWrapper(false);
Company comapny = objectMapper.readValue(xml,Company.class);

将 xml 转换为 pojo 时出现以下错误

线程“main”com.fasterxml.jackson.databind.exc.MismatchedInputException 中的异常:无法构造 com.test.Employee 的实例(尽管至少存在一个 Creator):没有用于反序列化的字符串参数构造函数/工厂方法字符串值 ('E1') 在 [来源: (StringReader); line: 4,column: 7] (通过引用链: com.test.Employee["Company"]->java.util.ArrayList[0])

解决方法

问题在于反序列化中全局 module.setDefaultUseWrapper(false) 的使用,而 DefaultUseWrapper 的使用应仅限于 List<Employee> employee 类中的嵌套集合 Company使用 @JacksonXmlElementWrapper(useWrapping = false) 如下所示:

@JacksonXmlRootElement
public class Company {
    
    @JacksonXmlProperty(localName = "Employee")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<Employee> employee;
    
    ...other fields,setters,getters and constructors from your code
}

您必须删除 module.setDefaultUseWrapper(false) 并重写您的主类,如下所示:

JacksonXmlModule module = new JacksonXmlModule();
XmlMapper objectMapper = new XmlMapper(module);
Company company = objectMapper.readValue(xml,Company.class);
System.out.println(objectMapper.writeValueAsString(company));