问题描述
我想序列化一个不受我控制的 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();
...
}
从例子中可以猜到,这个类是由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:
使用像这样的混合技术忽略来自超类的特定字段:如何告诉 jackson 忽略我无法控制源代码的属性?
这样做的问题是,如果基类发生变化(例如,其中出现了一个新的 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?
但是,从纯设计的角度来看,最好能够告诉 jackson 我只想序列化最终类的属性,而忽略所有继承的属性.有没有办法做到这一点?
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?
提前致谢.
推荐答案
您可以注册一个自定义的Jackson annotation introector 将忽略来自特定超类型的所有属性.下面是一个例子:
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));
}
}
输出:
{字段2":b"}
这篇关于Jackson 序列化:如何忽略超类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!