The following method convertToObject uses Jackson to convert an XML to an Object. I get the following error:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.test.Student`
(although at least one Creator exists):no String-argument constructor/factory method to deserialize from String value ('12')
Here are the core classes:
private Students convertToObject(String xml) {
XmlMapper xmlMapper = new XmlMapper();
Students matchedObjects = null;
try {
System.out.println(xml);
matchedObjects = xmlMapper.readValue(xml, Students.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(matchedObjects);
return matchedObjects;
}
This is the input XML:
<students>
<student><id>12</id><surname>DOE</surname><givenName>JOHN</givenName><marks>98.928</marks></student>
<student><id>13</id><surname>PRICE</surname><givenName>MIKE</givenName><marks>90.8444</marks> </student>
</students>
These are the two POJO's:
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
@JacksonXmlRootElement(localName = "student")
public class Student {
String id;
String givenName;
String surname;
String marks;
}
@Getter @Setter @AllArgsConstructor @NoArgsConstructor @ToString
@JacksonXmlRootElement(localName = "students")
public class Students {
@JacksonXmlProperty(localName = "student")
private List<Student> student;
}
This is the dependency in the pom file.
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>