Jackson annotation to override the one in parent class Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Jackson annotation to override the one in parent class without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I would like all my objects to have an ID and I wish to serialise it for some child class but not for some others

for example:

public class A {
  protected Long id;
  ..getter and setter
}

public class B extends A {

  @Override
  @JsonIgnore
  public Long getId() {..}

  @Override
  @JsonIgnore
  public void setId(Long id) {..}

}

public class C extends B {

  @Override
  @JsonInclude
  public Long getId() {..}

  @Override
  @JsonInclude
  public void setId(Long id) {..}
}

public class Test {

  Set<C> tests ...
  ..getter setter
}

I have tried serialising Test but the JSON string doesn’t include the IDs If I remove the JsonIgnore from B then in that case the Ids are there.

Is there a way with jackson to archive this?

Answer

Use

@JsonIgnore(false)

instead of

@JsonInclude
We are here to answer your question about Jackson annotation to override the one in parent class - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji