今天遇到一个同事的求助,他在写一个RestAPI的接口,测试时遇到错误如下:

1
2
3
4
5
6
7
8
9
root cause com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot onstruct instance of `com.zeusas.cloud.activiti.bean.ProcessDrawBean` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (PushbackInputStream); line: 1, column: 2]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) ~[jackson-databind-2.9.8.jar:2.9.8]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084) ~[jackson-databind-2.9.8.jar:2.9.8]

是Jackson在反序列化的时候找不到 com.zeusas.cloud.activiti.bean.ProcessDrawBean 的无参构造函数,查看其代码发现他有定义构造函数,但没有生命无参的构造函数。

解决方案

添加无参的构造函数。

告诉他当我们没有声明构造函数时,有默认的构造函数,但是当我们声明了一个构造函数时,JVM便不再生成默认的构造函数了。
对方回答: 这种简单的东西 我知道!。
我…