If you are working on Jackson library and trying to marshal/unmarshal Java objects to/from JSON documents, you may encounter JsonMappingException error.
1. JsonMappingException
When we are parsing a JSON file into Java object or we are converting the Java object to JSON representation, we must have created a POJO class. During marshalling or unmarshalling we may face this issue.
The exception stack-trace for JsonMappingException
looks like this:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class test.jackson.Employee]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: c:\temp\employee.json; line: 1, column: 2]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:483)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:350)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2395)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1549)
at test.jackson.JSONToJavaExample.main(JSONToJavaExample.java:19)
2. Solution
In runtime, Jackson uses the default constructor to create the instance of POJO classes.
To solve this exception, all you have to do is to create the default constructor for your POJO class which you are trying to marshal/unmarshal. This simple solution will fix this error.
You are missing default constructor in your POJO class. Provide one.
Happy Learning !!
Comments