问题描述
我想序列化一个不受我控制的POJO类,但希望避免序列化来自超类的任何属性,而不是来自最终类。示例:
I want to serialize a POJO class which is not under my control, but want to avoid serializing any of the properties which are coming from the superclass, and not from the final class. Example:
public class MyGeneratedRecord extends org.jooq.impl.UpdatableRecordImpl<...>,
example.generated.tables.interfaces.IMyGenerated {
public void setField1(...);
public Integer getField1();
public void setField2(...);
public Integer getField2();
...
}
您可以从示例中猜出这是class是由JOOQ生成的,并且继承自复杂的基类UpdatableRecordImpl,它也有一些类似bean属性的方法,这会在序列化过程中引起问题。另外,我有几个类似的类,所以最好避免为我生成的所有POJO重复相同的解决方案。
You can guess from the example that that this class is generated by JOOQ, and inherits from a complex base class UpdatableRecordImpl which also has some bean property-like methods, which cause problems during the serialization. Also, I have several similar classes, so it would be good to avoid duplicating the same solution for all of my generated POJOs.
到目前为止,我找到了以下可能的解决方案:
I have found the following possible solutions so far:
-
使用mixin技术忽略来自超类的特定字段,如下所示:
这个问题是如果基类发生了变化(例如,一个新的getAnything()方法出现在它中),它可能会破坏我的实现。
The problem with this is that if the base class changes (e.g., a new getAnything() method appears in it), it can break my implementation.
实现自定义序列化程序并在那里处理问题。这对我来说似乎有点矫枉过正。
implement a custom serializer and handle the issue there. This seems a bit overkill to me.
顺便提一句,我有一个界面,它描述了我想要序列化的属性,也许我可以混合@JsonSerialize( as = IMyGenerated.class)注释......?我可以将它用于我的目的吗?
as incidentally I have an interface which describes exactly the properties I want to serialize, maybe I can mixin a @JsonSerialize(as=IMyGenerated.class) annotation...? Can I use this for my purpose?
但是,从纯粹的设计角度来看,最好的是能够告诉杰克逊我只想序列化最后一个类的属性,并忽略所有继承的属性。有没有办法做到这一点?
But, from pure design point of view, the best would be to be able to tell jackson that I want to serialize only the final class' properties, and ignore all the inherited ones. Is there a way to do that?
提前致谢。
推荐答案
您可以注册一个自定义的,它将忽略来自特定超级的所有属性类型。下面是一个示例:
You can register a custom Jackson annotation intropector which would ignore all the properties that come from the certain super type. Here is an example:
public class JacksonIgnoreInherited {
public static class Base {
public final String field1;
public Base(final String field1) {
this.field1 = field1;
}
}
public static class Bean extends Base {
public final String field2;
public Bean(final String field1, final String field2) {
super(field1);
this.field2 = field2;
}
}
private static class IgnoreInheritedIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean hasIgnoreMarker(final AnnotatedMember m) {
return m.getDeclaringClass() == Base.class || super.hasIgnoreMarker(m);
}
}
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new IgnoreInheritedIntrospector());
final Bean bean = new Bean("a", "b");
System.out.println(mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(bean));
}
}
输出:
{
field2:b
}
{ "field2" : "b"}
这篇关于杰克逊序列化:如何忽略超类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!