Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override @JsonIgnore in a subclass

I have the following abstract class:

public abstract class StandardTimeStamp {

  @Temporal(TemporalType.TIMESTAMP)
  @Column(nullable = false)
  @JsonIgnore
  private Date lastUpdated;

  @PreUpdate
  public void generatelastUpdated() {
    this.lastUpdated = new Date();
  }

  public Date getLastUpdated() {
    return lastUpdated;
  }

  public void setLastUpdated(Date lastUpdated) {
    this.lastUpdated = lastUpdated;
  }
}

I have an entity that is a subclass in which I need the lastUpdate value to be sent to the browser, so I need to override the @JsonIgnore. I have tried a few things, the following code being one of them:

public class MyEntity extends StandardTimestamp implements Serializable {

  @Column(name = "LastUpdated")
  private Date lastUpdated;

  @JsonIgnore(false)
  @Override
  public Date getLastUpdated() {
    return lastUpdated;
  }
}

This does add the lastUpdated attribute on the JSON response, however its value is null when it is not null in the database. I have other Dates in the subclass that work, but they aren't hidden with @IgnoreJson in the super class.

Any thoughts?

like image 289
Nick Suwyn Avatar asked May 06 '16 14:05

Nick Suwyn


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

You're introducing a second field named lastUpdated with this declaration

@Column(name = "LastUpdated")
private Date lastUpdated;

in your subtype MyEntity. I'm not sure how the database mapper handles this, but it seems redundant to me.

I can see two options.

One, get rid of this new field and have your overriden getLastUpdated method delegate to its super implementation. For example

@Override
@JsonIgnore(false)
@JsonProperty
public Date getLastUpdated() {
    return super.getLastUpdated();
}

When serializing a MyEntity object, Jackson will discover a single property through this accessor, lastUpdated, and use it to generate the JSON. It'll end up retrieving it from the superclass.

Two, use the class annotation @JsonIgnoreProperties to mark the ignored properties in the superclass

@JsonIgnoreProperties("lastUpdated")
abstract class StandardTimeStamp {

then clear (or add different values to) that "list" in the subclass

@JsonIgnoreProperties()
class MyEntity extends StandardTimeStamp implements Serializable {

Jackson will only use the annotation on the subclass, if it's present, and ignore the annotation on the superclass.

like image 58
Sotirios Delimanolis Avatar answered Oct 04 '22 12:10

Sotirios Delimanolis