Immutable Lombok annotated class with Jackson

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

Edit: 2020-08-16

  • Note: Using @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

  • Note: When you add or change a 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.

As of 15 October 2020 (Lombok v1.18.16), you should simply be able to use the @Jacksonized annotation.

@Jacksonized @Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class JacksonExample {
  private List<Foo> foos;
}

As described in the linked documentation, this annotation:

  • configures Jackson to use the builder for deserialisation,
  • copies field-specific configuration down from the annotated class to the generated builder (e.g. @JsonIgnoreProperties), and
  • aligns the Jackson prefix used on builder methods (e.g. builder().withField(field) vs builder.field(field) to the one configured in Lombok.