Lombok v1.6.20不适用于杰克逊

时间:2018-02-12 14:50:21

标签: java lombok jackson2

当使用最新的Lombok v1.6.20生成的构造函数时,Jackson无法使用构造函数。

例如,反序列化时

@Data
@AllArgsConstructor
private static class TestObject {
    private Integer a;
    private String b;
}

我正在

    com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of TestObject: 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: {"a":0,"b":"0"}; line: 1, column: 2]

    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
    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:1206)
    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:3814)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2858)

它适用于Lombok 1.6.18。迁移到Spring Boot 2时可能会发生此问题。

1 个答案:

答案 0 :(得分:6)

原因是Lombok 1.6.20中的后向不兼容变化。有关详细信息,请参阅Lombok changelog https://projectlombok.org/changelog和此错误报告https://github.com/rzwitserloot/lombok/issues/1563

解决方案是指定lombok.anyConstructor.addConstructorProperties配置参数。