Jackson XML deserialization error: no String-argument constructor/factory method to deserialize from String value ('checka'),

Viewed 105

I am trying to deserialize a simple xml with pojo but getting error as below: Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of classname (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('checka') at [Source: (com.ctc.wstx.sr.ValidatingStreamReader); line: 3, column: 15] (through reference chain: classname["testData"]->java.util.ArrayList[0]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)

<tc>
<testData>
    <post>checka</post>
    <xtenantid>netwebshopa</xtenantid>
    <jsonbody>Testa</jsonbody>
</testData>
<testData>
    <post>check</post>
    <xtenantid>netwebshop</xtenantid>
    <jsonbody>Test</jsonbody>
</testData>
@JacksonXmlRootElement(localName = "tc")
public class TC {

private List<TestData> testData;

public List<TestData> getTestData() {
    return testData;
}

public void setTestData(List<TestData> testData) {
    this.testData = testData;
}



}


public class TestData {
    
    
private String post;
private String xtenantid;
private String jsonbody;

public String getPost() {
    return post;
}
public void setPost(String post) {
    this.post = post;
}
public String getXtenantid() {
    return xtenantid;
}
public void setXtenantid(String xtenantid) {
    this.xtenantid = xtenantid;
}
public String getJsonbody() {
    return jsonbody;
}
public void setJsonbody(String jsonbody) {
    this.jsonbody = jsonbody;
}


    
}
2 Answers

You need a constructor with all parameters:

public class TestData(String post, String xtenantid, String jsonbody) {
    
    
this.post= post;
this.xtenantid= xtenantid;
this.jsonbody= jsonbody;

If you try to deserialize the class. You get something like this.

<tc>
<testData>
    <testData>
        <post>checka2</post>
        <xtenantid>netwebshopa2</xtenantid>
        <jsonbody>Testa2</jsonbody>
    </testData>
    <testData>
        <post>checka1</post>
        <xtenantid>netwebshopa1</xtenantid>
        <jsonbody>Testa1</jsonbody>
    </testData>
</testData>

The TestData class is duplicated because jackson uses elementWrapper by default. To remove it, you must use @JacksonXmlElementWrapper(useWrapping = false).

The result should look like this.

public class ParrentXml {

    @JacksonXmlElementWrapper(useWrapping = false)
    List<ChildXml> childxmls = new ArrayList<>();

    public List<ChildXml> getChildxmls() {
        return childxmls;
    }

    public void setChildxmls(List<ChildXml> childxmls) {
        this.childxmls = childxmls;
    }
}
Related