I have a class Parent
public class Parent { private int id; @JsonIgnore int getId() {} void setId(int id) {} }
I have a subclass which is derived from Parent
public class Child extends Parent { @JsonProperty // just to explicitly tell jackson to serialize this @Override int getId() {} @Override void setId(int id) {} }
I actually don't want the id property to be serialized when an object of Parent is returned but it should be serialized when an object of Child class is returned.
I think if Parent was an Interface, overriding the visibility would work, but I am not sure if the behavior is the same with superclass.
Is there a simple solution for this? I would really appreciate your answers. Tx.
This can be done to override an existing JsonIgnore by explicitly defining one with 'false' argument.
@JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setters, getters or fields. If you add @JsonIgnore to a field or its getter method, the field is not going to be serialized.
The. @JsonIgnore. annotation marks a field in a POJO to be ignored by Jackson during serialization and deserialization. Jackson ignores the field in both JSON serialization and deserialization.
What you want in the Child class is not @JsonProperty
but instead @JsonIgnore(false)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With