Jackson annotation to override the one in parent class

别说谁变了你拦得住时间么 提交于 2020-01-15 10:47:29

问题


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?


回答1:


Use

@JsonIgnore(false)

instead of

@JsonInclude


来源:https://stackoverflow.com/questions/37839796/jackson-annotation-to-override-the-one-in-parent-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!