我正在调用数据库存储过程,该过程将输出返回为XML。
数据库输出
<ROWSET>
<ROW>
<EMPLOYEEID>1</EMPLOYEEID>
<EMPLOYEENAME>SEAN</EMPLOYEENAME>
<DEPT>ACOUNTING</DEPT>
</ROW>
<ROW>
<EMPLOYEEID>6</EMPLOYEEID>
<EMPLOYEENAME>KAREN</EMPLOYEENAME>
<DEPT>HR</DEPT>
</ROW>
</ROWSET>
我正在使用Jackson将XML String转换为java对象。我创建了一个支持java类来将XML映射到java对象
@JacksonXmlRootElement(localName = "ROWSET")
public class RowSet {
@JacksonXmlProperty(localName = "ROW")
private Row [] row;
public RowSet() {
}
public Row [] getRow() {
return row;
}
public void setRow(Row [] row) {
this.row = row;
}
}
class Row {
@JacksonXmlProperty(localName = "EMPLOYEEID")
private String employeeId;
@JacksonXmlProperty(localName = "EMPLOYEENAME")
private String employeeName;
@JacksonXmlProperty(localName = "DEPT")
private String dept;
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
从字符串xml创建Java对象的代码
public static void main(String ... args){
String ouputput= getEmployeeData();// DB call to get data as xml string
XmlMapper xmlMapper = new XmlMapper();
RowSet rowSet= xmlMapper.readValue(ouputput, RowSet.class);
System.out.println(rowSet.getRow().length);
}
我遇到以下异常
com.fasterxml.jackson.databind.JsonMappingException:无法构造com.org.employee.Row的实例,没有用于从字符串值('1')反序列化的字符串参数构造函数/工厂方法
答案 0 :(得分:1)
XML中的this.setState({ text: ' ' });
数组应该具有一个包装(除了Row
之外),该包装在您的XML文本中不存在。 Tell Jackson不要寻找包装器,例如
RowSet