Maven Dependancy - Eclipse IDE
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
Without any annotation, I am getting
Console Error
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "id" (class com.optimight.prakash.general.Employee), not marked as ignorable (0 known properties: ]) at [Source: (String)"{"id":1,"name":"Lokesh Gupta","age":34,"location":"India"}"; line: 1, column: 8] (through reference chain: com.optimight.prakash.general.Employee["id"]) at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61) at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:823) at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1153) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1589) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1567) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004) at com.optimight.prakash.general.JSONtoJavaPOJO.main(JSONtoJavaPOJO.java:18)
With annotation: @JsonIgnoreProperties(ignoreUnknown = true)
in Employe Class , then I am getting
Employee [id=null, name=null, age=null, location=null]
Application Class - Main Method
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONtoJavaPOJO {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"Lokesh Gupta\",\"age\":34,\"location\":\"India\"}";
ObjectMapper mapper = new ObjectMapper();
try
{
Employee emp = mapper.readValue(json, Employee.class);
System.out.println(emp);
}
catch (JsonGenerationException e)
{
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Employee Class
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
//@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee
{
private Integer id;
private String name;
private Integer age;
private String location;
public Employee() {
}
public Employee(Integer id, String name, Integer age, String location) {
super();
this.id = id;
this.name = name;
this.age = age;
this.location = location;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", location=" + location + "]";
}
}
Employee
class? How are you getting the data back out with private members and no getters? Anyway, I suspect that Jackson is using the no-arg constructor to instantiate your object, and is then unable to set any of the values because there are no setters. Try adding setters and see if that fixes the issue. - Jordan