本文介绍了Jackson注释覆盖父类中的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的所有对象都有一个ID,我希望将其序列化为某些子类,但不能用于其他一些
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
例如:
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
}
我试过序列化测试,但JSON字符串不包含ID
如果我从B中删除JsonIgnore,那么在那种情况下,Ids就在那里。
I have tried serialising Test but the JSON string doesn't include the IDsIf I remove the JsonIgnore from B then in that case the Ids are there.
有没有办法与杰克逊存档这个?
Is there a way with jackson to archive this?
推荐答案
使用
@JsonIgnore(false)
而不是
@JsonInclude
这篇关于Jackson注释覆盖父类中的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!