Lombok - Jackson (de)serializing immutable (@Value) objects
Current Jackson version (2.9.7) and Lombok version (1.18.2) allow (de)serializing immutable objects from Java version 8 when using the Java 8 named parameters option. This is a compiler flag for javac -parameters
which makes method parameter names available via reflection.
Enabling this in Gradle:
tasks.withType(JavaCompile) {
options.compilerArgs << '-parameters'
}
The jackson-module-parameter-names
module is also required.
compile "com.fasterxml.jackson.module:jackson-module-parameter-names"
//OR
compile "com.fasterxml.jackson.module:jackson-modules-java8"
Pitfall
There is currently a bug where immutable objects containing a single property cannot be (de)serialized out of the box. The workaround is to use the following Lombok annotation:
@AllArgsConstructor(onConstructor_= {@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)})