Jackson ignore fields

Suppose we have a java class as below

public class Glass {

  private String material;

  private float height;

  // getter and setters
}

When an object of this class is serialized, that is, it is converted to a json string with Jackson or it is sent as a json response from a spring boot application, it will be in below format

{"material":"plastic","height":6}

Assume that we don’t want to send height field in response. This article will cover different ways to do that.

1. @JsonIgnore
Apply @JsonIgnore annotation over the field or its getter method to remove it from json string as shown below

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Glass {
 
  private String material; 

  @JsonIgnore
  private float height; 

  // getter and setters 
}

Now, whether it is json response returned by a spring boot application or an object converted to a json string, it will not have the ignored field.
2. @JsonIgnoreProperties
This annotation is applied over the class. It may be used to remove a single field or multiple fields at once.
Single field can be ignored by writing its name after annotation as shown below

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties("height")
public class Glass { 
  // other code
}

To ignore multiple properties, their names need to be provided as an array as shown below

@JsonIgnoreProperties({"property1", "property2"})

// this is also valid
@JsonIgnoreProperties(value={"property1", "property2"})

// single property can also be given as
@JsonIgnoreProperties({"property1"})

// or as
@JsonIgnoreProperties(value={"property1"})

3. Remove getter method
To prevent a field from being sent in response or from being serialized, remove its getter method as shown below

public class Glass {
  private String material; 

  private float height;

  
  public String getMaterial() {
    return material;
  }

  // no getter for height
}

But remember that the field itself should be private.
4. Private getter method
Another way to remove a field from being serialized is by making its getter method private as shown below.

public class Glass { 
  private String material; 

  private float height; 

  public String getMaterial() {
    return material;
  }

  private String getHeight() {
    return height;
  }
}

5. @JsonProperty
Apply @JsonProperty annotation over a field and set its access attribute. This attribute accepts a value of type Access which is a java enum.
Set the access attribute to WRITE_ONLY option of Access as shown below.

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;

public class Glass { 
  private String material; 

  @JsonProperty(access = Access.WRITE_ONLY)
  private float height; 

  // getters and setters
}

Documentation for WRITE_ONLY option states

Access setting that means that the property may only be written (set) as part of deserialization (using “setter” method, or assigning to Field, or passed as Creator argument) but will not be read (get) for serialization, that is, the value of the property is not included in serialization

That is all for this article on different ways to remove or ignore a field from json string using Jackson.

0
Liked the article ? Spread the word...