没有从字符串值反序列化的字符串参数 constructor/factory 方法 ('E1')
no String-argument constructor/factory method to deserialize from String value ('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>
Company.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
时出现以下错误
线程“主”中的异常com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.test.Employee
的实例(尽管至少存在一个创建者):没有字符串参数constructor/factory方法从字符串值反序列化('E1')
在 [来源:(StringReader);行:4,列:7](通过参考链:com.test.Employee[“公司”]->java.util.ArrayList[0])
问题在于反序列化中全局 module.setDefaultUseWrapper(false)
的使用,而 DefaultUseWrapper
的使用应限于 [=15= 中的嵌套集合 List<Employee> employee
] class 与 @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)
并重写您的主要 class,如下所示:
JacksonXmlModule module = new JacksonXmlModule();
XmlMapper objectMapper = new XmlMapper(module);
Company company = objectMapper.readValue(xml,Company.class);
System.out.println(objectMapper.writeValueAsString(company));
无法将 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>
Company.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
时出现以下错误线程“主”中的异常com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造com.test.Employee
的实例(尽管至少存在一个创建者):没有字符串参数constructor/factory方法从字符串值反序列化('E1')
在 [来源:(StringReader);行:4,列:7](通过参考链:com.test.Employee[“公司”]->java.util.ArrayList[0])
问题在于反序列化中全局 module.setDefaultUseWrapper(false)
的使用,而 DefaultUseWrapper
的使用应限于 [=15= 中的嵌套集合 List<Employee> employee
] class 与 @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)
并重写您的主要 class,如下所示:
JacksonXmlModule module = new JacksonXmlModule();
XmlMapper objectMapper = new XmlMapper(module);
Company company = objectMapper.readValue(xml,Company.class);
System.out.println(objectMapper.writeValueAsString(company));