Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson: Ignore parent class properties

Is there any way I can tell Jackson to ignore properties from parent class while serializing a child class?


    class Parent{
      private String parentProperty1;
      private String parentProperty2;
      //getter setter
    }

    @IgnoreParentProperties // I am expecting something like this
    class Child extends Parent{
      private String childProperty1;
      //getter setter
    }

like image 597
Moinul Hossain Avatar asked Dec 30 '11 06:12

Moinul Hossain


People also ask

How do I ignore properties in Jackson?

Ignore All Fields by Type Finally, we can ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly: @JsonIgnoreType public class SomeType { ... } More often than not, however, we don't have control of the class itself.

How do I ignore properties in JSON?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do I ignore a field in JSON response Jackson?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON.

How do I ignore a class in Jackson JSON?

Jackson JSON - Using @JsonIgnoreType to ignore whole class. The annotation @JsonIgnoreType can be used to ignore the whole class, i.e. to ignore all properties of that class. However, that only applies to the places where that type is used as property.

Does Jackson ignore the name property of an object?

Here, Jackson doesn't ignore the name property and serializes it. Now let's continue with deserialization-only objects. When we set allowSetters as true, Jackson treats the object as write-only. In other words, Jackson will ignore the property for serialization but will use it for deserialization.

How to ignore all properties of a class in Java?

The annotation @JsonIgnoreType can be used to ignore the whole class, i.e. to ignore all properties of that class. However, that only applies to the places where that type is used as property. @JsonIgnoreType public class Address { private String street; private String city; ............. }

How to ignore JSON property if not defined in Pojo?

@JsonIgnoreProperties#ignoreUnknown can be used to ignore a JSON property if is not defined in the POJO: @JsonIgnoreProperties(ignoreUnknown = true) public class Employee3 { private String name; private String dept; private String address; .............


2 Answers

In addition to Views that work well, you can also use @JsonIgnoreProperties to list names of properties to ignore; this can include parent properties as well.

like image 175
StaxMan Avatar answered Sep 16 '22 12:09

StaxMan


Define & use a JSON view which omits the inherited fields.

like image 26
Matt Ball Avatar answered Sep 20 '22 12:09

Matt Ball