Reputation: 1287
I am calling the database stored procedure which returns the output as XML.
DB output
<ROWSET>
<ROW>
<EMPLOYEEID>1</EMPLOYEEID>
<EMPLOYEENAME>SEAN</EMPLOYEENAME>
<DEPT>ACOUNTING</DEPT>
</ROW>
<ROW>
<EMPLOYEEID>6</EMPLOYEEID>
<EMPLOYEENAME>KAREN</EMPLOYEENAME>
<DEPT>HR</DEPT>
</ROW>
</ROWSET>
I am using Jackson to convert XML String to java objects. I have created a supporting java class to map XML to java objects
@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;
}
}
Code to create java object from string xml
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);
}
I am getting the following exception
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.org.employee.Row no String-argument constructor/factory method to deserialize from String value ('1')
Upvotes: 1
Views: 847
Reputation: 22214
The Row
array in XML is supposed to have a wrapper (apart from RowSet
) that doesn't exist in you XML text. Tell Jackson to not look for a wrapper e.g.
@JacksonXmlProperty(localName = "ROW")
@JacksonXmlElementWrapper(useWrapping = false)
private Row[] row;
Upvotes: 1