I have a project which uses both Jackson and Lombok. It is using the current latest versions of both (Jackson 2.12.2 & Lombok 1.18.18). I have the following class containing a Jackson mapped object, with an associated Jackson mixin, allowing it to be deserialized either ignoring the given field or not, depending on whether the mixin is passed to the ObjectMapper:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
public class IssueDemonstration {
public static void main(String[] args) throws JsonProcessingException {
JsonMapper jsonMapper = JsonMapper.builder().build();
JsonMapper mixinMapper = JsonMapper.builder()
.addMixIn(Example.class, ExampleMixin.class).build();
String json = "{\"value\": \"Some Value\"}";
System.out.println(
"Default: " + jsonMapper.readValue(json, Example.class).getValue());
System.out.println(
"Mixin: " + mixinMapper.readValue(json, Example.class).getValue());
}
private static class Example {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
private abstract static class ExampleMixin {
@JsonProperty(access = JsonProperty.Access.READ_WRITE)
private String value;
}
}
The above code gives the expected results:
Default: null
Mixin: Some Value
However, if I change Example to use Lombok's @Data to automagically generate the getter & setter, the mixin no longer works:
@Data
private static class Example {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String value;
}
Output:
Default: null
Mixin: null
Why is this not working when I switch to using @Data? I see nothing in the Jackson @Data or @Getter and @Setter documentation that indicate that this should behave in this manner.