Jackson serialization: how to ignore superclass properties

后端 未结 5 928
走了就别回头了
走了就别回头了 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:10

    You can use this as well instead of unnecessary overrides

    @JsonIgnoreProperties({ "aFieldFromSuperClass"})
    public class Child extends Base {
       private String id;       
       private String name; 
       private String category;
    } 
    
    0 讨论(0)
  • 2020-12-10 01:11

    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));
        }
    
    }
    

    Output:

    { "field2" : "b" }

    0 讨论(0)
  • 2020-12-10 01:12

    The good use of inheritance is that the child classes extend or add functionality. So the usual way is to serialize the data.

    A workarround would be to use a Value Object (VO) or Data Transfer Object (DTO) with the fields you need to serialize. Steps:

    • Create a VO class with the fields that should be serialized.
    • Use BeanUtils.copyProperties(target VO, source data) to copy the properties
    • Serialize the VO instance.
    0 讨论(0)
  • 2020-12-10 01:14

    Add the following annotation in your Base Class :

    @JsonInclude(Include.NON_NULL)
    
    0 讨论(0)
  • 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();
      };
    ...
    }
    
    0 讨论(0)
提交回复
热议问题