Search code examples
javajacksonxml-parsingpojojackson-dataformat-xml

no String-argument constructor/factory method to deserialize from String value ('E1')


Not able to convert xml to java pojo object. please find below details:-

Input 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 has dependancy:-

<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;
    }
    
    

}

Converting xml to java code:-

JacksonXmlModule module = new JacksonXmlModule();
XmlMapper objectMapper = new XmlMapper(module);
module.setDefaultUseWrapper(false);
Company comapny = objectMapper.readValue(xml,Company.class);

Below error i am getting while converting xml to pojo

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.test.Employee (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('E1') at [Source: (StringReader); line: 4, column: 7] (through reference chain: com.test.Employee["Company"]->java.util.ArrayList[0])


Solution

  • The problem stands in the use of the global module.setDefaultUseWrapper(false) in your deserialization while the use of the DefaultUseWrapper should be limited to the nested collection List<Employee> employee in your Company class with the @JacksonXmlElementWrapper(useWrapping = false) like below:

    @JacksonXmlRootElement
    public class Company {
        
        @JacksonXmlProperty(localName = "Employee")
        @JacksonXmlElementWrapper(useWrapping = false)
        private List<Employee> employee;
        
        ...other fields, setters, getters and constructors from your code
    }
    

    You have to delete the module.setDefaultUseWrapper(false) and rewrite your main class like below:

    JacksonXmlModule module = new JacksonXmlModule();
    XmlMapper objectMapper = new XmlMapper(module);
    Company company = objectMapper.readValue(xml,Company.class);
    System.out.println(objectMapper.writeValueAsString(company));