Jackson serialization: how to ignore superclass properties

后端 未结 5 930
走了就别回头了
走了就别回头了 2020-12-10 00:58

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 fi

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 01:18

    You can override the superclass' methods which you'd like to prevent from being output and annotate them with @JsonIgnore. The override shifts the control of property creation to the subclass while enabling its ability to filter it from the output.

    For instance:

    public class SomeClass {
      public void setField1(...);
      public Integer getField1();
    
      public void setField2(...);
      public Integer getField2();
    
      @Override
      @JsonIgnore
      public String superClassField1(...){
          return super.superClassField1();
      };
    
      @Override
      @JsonIgnore
      public String superClassField2(...){
          return super.superClassField2();
      };
    ...
    }
    

提交回复
热议问题