Christian Neverdal :
What is the preferred way to create class that is
- Immutable
- Can be serialized/deserialized with Jackson
- Human-readable and with low level of boilerplate
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
robSE13 :
add ConstructorProperties:
- Create a
lombok.config
file in an appropriate location with the line:lombok.anyConstructor.addConstructorProperties = true
- Add lombok
@Value
annotation to your class to make it immutable
Then serialization and deserialization by Jackson works as expected.
This method:
- meets the criteria
- has less boilerplace than the previous top answer
- works on v1.16.20 (January 9th, 2018) and later
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=432013&siteId=1