What is the preferred way to create class that is
Preferably, I would have liked something like this to work:
@Data(onConstructor = @__(@JsonCreator))
and then have all fields to be private final
. However, this does not even compile (and I'm not sure why). Using
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
will compile but only yields
InvalidDefinitionException: No serializer found for class
add ConstructorProperties:
lombok.config
file in an appropriate location with the line:
lombok.anyConstructor.addConstructorProperties = true
@Value
annotation to your class to make it immutableThen serialization and deserialization by Jackson works as expected.
This method:
Edit: 2020-08-16
@Builder
with @Value
causes this solution to fail. (Thanks to comment from @guilherme-blanco below.)
However, if you also add e.g. @AllArgsConstructor
it does still work as expected.Edit: 2021-08-19
lombok.config
file the change is not picked up unless you do a rebuild (clean then build). I have been caught out by this a few times.@Jacksonized
annotation solution is another method to achieve the desired outcome for the specific classes annotated. However, I personally prefer not to need to remember to annotate every class used for deserialization. Using lombok.config
removes this overhead.