Reputation: 369
I need to deserialize some XML file to regular java object using jackson-dataformat-xml. So I'm doing:
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
XmlMapper mapper = new XmlMapper();
return mapper.readValue(xmlString, Certificate.class);
xmlString has appearence:
<?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>
And class Certificate:
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;
}
}
}
}
After trying to deserealize XML, I got the exception:
"No suitable constructor found for type [simple type, class ua.max.Certificate$R]: can not instantiate from JSON object"
My attempts:
1. If I add modifier "static" for my inner classes it's working, I get java object, but except List of 2 object "ATT-SEARCH" I got the first one is null
2. The addition of different constructors did not make any effect
Upvotes: 3
Views: 4568
Reputation: 1493
R
and AttSearch
should be static:
public static class R {
// other stuff
public static class AttSearch {
// other stuff
Otherwise compiler create default constructor with outer class reference as parameter, so fasterxml can't find constructor without parameters and create pojo.
Upvotes: 3