Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding @jsonIgnore in subclass

Tags:

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.

like image 828
Nishant Nagwani Avatar asked Apr 11 '13 18:04

Nishant Nagwani


People also ask

How do I override JsonIgnore?

This can be done to override an existing JsonIgnore by explicitly defining one with 'false' argument.

When should I use JsonIgnore?

@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.

What is @JsonIgnore in spring boot?

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.


1 Answers

What you want in the Child class is not @JsonProperty but instead @JsonIgnore(false).

like image 121
Pascal Gélinas Avatar answered Oct 07 '22 17:10

Pascal Gélinas