Javatpoint Logo
Javatpoint Logo

Jackson Ignore Properties on Marshalling

In this section, we will understand how we can ignore the fields at the time of serializing an object into JSON. There are various ways to ignore the properties which we use when Jackson defaults are not enough to control what gets serialized to JSON.

Ignore fields at the class level

At the class level, we can easily ignore the specific fields by using the @JsonIgnoreProperties and specifying the fields by name. Let's take an example to understand how we use this annotation to ignore fields at the class level.

IgnoreFieldExample1.java

Output:

Jackson Ignore Properties on Marshalling

Ignore fields at the field level

At the field level, we can directly ignore the field with the help of the @JsonIgnore. Let's take an example to understand how we use this annotation to ignore fields at the field level.

IgnoreFieldExample2.java

Output:

Jackson Ignore Properties on Marshalling

Ignore all fields by type

In this way, we ignore all the fields of a specified type. We can directly annotate the class if we can control the type.

All the time, however, we don't have control of the class itself, so we can use Jackson mixins too. We use the following steps to use mixins:

1. Define mixin for the type we would like to ignore.

2. Annotate mixin with @JsonIgnoreType annotation.

3. Register mixin for ignoring all Address types during marshalling.

Let's take an example to understand how we can use mixin to ignore fields of Address type.

IgnoreFieldExample3.java

Output:

Jackson Ignore Properties on Marshalling

Ignore fields by using filters

By using the filters, we can also ignore the specific fields in Jackson. In order to use the filter, we need to define it on the Java object in the following way:

After that, we define a simple filter to ignore the intValue field:

Let's take an example to understand how we can use a filter to ignore the intValue field.

IgnoreFieldExample4.java

Output:

Jackson Ignore Properties on Marshalling





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA