ObjectMapper将json转对象报错处理

Posted 仅此而已-远方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ObjectMapper将json转对象报错处理相关的知识,希望对你有一定的参考价值。

在使用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":1,"message":"Hello World","sendTime":1525162183195,"intList":null}; line: 1, column: 2]
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
    at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1456)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1012)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1203)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:314)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
    at testJson.TestObjectMapper.testObj(TestObjectMapper.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  ....

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

 



以上是关于ObjectMapper将json转对象报错处理的主要内容,如果未能解决你的问题,请参考以下文章

Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method

Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method

Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method

Json字符串转对象,使用ObjectMapper方式报错:no String-argument constructor/factory method

jackson怎么将list转为json字符串

JSON对象与字符串相互转化ObjectMapper