When using constructor generated by newest Lombok v1.6.20, Jackson is not able to use the constructor.
For example, when deserializing
@Data
@AllArgsConstructor
private static class TestObject {
private Integer a;
private String b;
}
I am getting
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)
It works in Lombok 1.6.18. The issue can happen when migrating to Spring Boot 2.
Lombok @Jacksonized. Using @Jacksonized annotation is the simplest solution if you are using Jackson for deserialization purposes. Just annotate the @Builder class with @Jacksonized annotation and we are good for converting the JSON string to Java object.
Looking at the Lombok change log , Java 17 support was added in 1.18. 22 . The version 1.18.
The @Jacksonized annotation is an add-on annotation for @Builder and @SuperBuilder . It automatically configures the generated builder class to be used by Jackson's deserialization. It only has an effect if present at a context where there is also a @Builder or a @SuperBuilder ; a warning is emitted otherwise.
Lombok has been around for quite a while now (first released in 2009) and has matured a lot since. However, I have always felt it deserved more attention—it is an amazing way of dealing with Java's natural verbosity.
The reason is backward incompatible change in Lombok 1.6.20. See Lombok changelog https://projectlombok.org/changelog and this bug report https://github.com/rzwitserloot/lombok/issues/1563 for more details
The solution is to specify lombok.anyConstructor.addConstructorProperties config parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With