InvalidDefinitionException is an intermediate exception which is used as the base class for all JsonMappingExceptions that occurred due to problems with target type definition. Generally a problem with properties or annotations used on a class.
Exception Example
Here this InvalidDefinitionException is occurring because of StudentDetail class is not having any getter and setters. That’s what while the time of deserialization found bean as empty.
public class StudentDetail { private int rollNumber; private String firstName; private String lastName; public StudentDetail(int rollNumber, String firstName, String lastName) { super(); this.rollNumber = rollNumber; this.firstName = firstName; this.lastName = lastName; }
import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class TestJacksonExample3 { public static void main(String[] args) { try { StudentDetail student=new StudentDetail(10011,"Saurabh","Gupta"); ObjectMapper mapper = new ObjectMapper(); //convert java object to JSON String json=mapper.writeValueAsString(student); System.out.println(json); } catch(IOException ex) { ex.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } } }
Exception Stacktrace
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class com.fiot.json.jackson.exceptions.StudentDetail and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:313)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3905)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)
at com.fiot.json.jackson.exceptions.TestJacksonExample3.main(TestJacksonExample3.java:17)
Solutions
There is a couple of solution to resolve this issue:
- If you have the option to edit source, add getter and setter of properties of the class.
- On class, level add annotation @JsonAutoDetect(fieldVisibility = Visibility.ANY) to StudentDetail class to resolve this issue.
@JsonAutoDetect(fieldVisibility = Visibility.ANY) public class StudentDetail { private int rollNumber; private String firstName; private String lastName; public StudentDetail(int rollNumber, String firstName, String lastName) { super(); this.rollNumber = rollNumber; this.firstName = firstName; this.lastName = lastName; }
- We can resolve this issue by setting configuring of ObjectMapper visibility for fields as Visibility.ANY.
try { StudentDetail student=new StudentDetail(10011,"Saurabh","Gupta"); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); //convert java object to JSON String json=mapper.writeValueAsString(student); System.out.println(json); } catch(IOException ex) { ex.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } }
You would like to see
Follow below link to learn more on JSON and JSON issues solutions:
You must be logged in to post a comment.