ObjectMapper将json转对象报错处理

时间:2024-11-15 18:05:13

在使用ObjectMapper将json转对象,调用mapper.readValue(jsonStr, XwjUser.class)时,报如下错:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of entity.XwjUser: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"id":,"message":"Hello World","sendTime":,"intList":null}; line: , column: ]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:)
at testJson.TestObjectMapper.testObj(TestObjectMapper.java:)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  ....

解决办法:在XwjUser实体类中,加入无参构造器,即可解决