@JsonIgnore Annotation in Java

The @JsonIgnore annotation is used to mark a property or a method that should be ignored by Jackson when serializing or deserializing a Java object to or from JSON. This annotation belongs to the Jackson library, which is a popular library for working with JSON in Java.

Spring Boot uses Jackson as the default JSON library, so you can use the @JsonIgnore annotation to exclude some properties from the JSON representation of your Spring Boot entities. For example, you may want to ignore some fields that contain sensitive or irrelevant information.

Using the @JsonIgnore annotation in Java

The @JsonIgnore annotation works at the field level and using it, we tell the Jackson to ignore the property during serialization and deserialization.

Let’s say we have this JSON message:

{
   "firstName":"John",
   "lastName":"Doe",
   "username":"johnDoe#1",
   "membershipType":"Platinum",
   "address": {
      "street":"5th Avenue",
      "city":"New York",
      "state":"New York"
   }
}


And we need two Java POJOs: Customer and Address.

Customer.java

import lombok.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {

  private String firstName;
  private String lastName;
  private String membershipType;
  private Address address;
}


Address.java

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {

  private String street;
  private String city;
  private String state;
}


Note: We used the Lombok annotations instead of writing the boilerplate codes such as constructors, getter, and setter methods.

Let’s convert the JSON message from above to the Customer object:

import com.fasterxml.jackson.databind.ObjectMapper;

public class Demo {

  public static void main(String[] args) throws JsonProcessingException {

    String jsonString = "{" +
            "\"firstName\":\"John\"," +
            "\"lastName\":\"Doe\"" +
            ",\"username\":\"johnDoe#1\"," +
            "\"membershipType\":\"Platinum\"," +
            "\"address\":{\"street\":\"5thAvenue\",\"city\":\"NewYork\",\"state\":\"NewYork\"}}";

    ObjectMapper objectMapper = new ObjectMapper();

    Customer customer = objectMapper.readValue(jsonString, Customer.class);

    System.out.println(customer);
  }
}
Output: Customer(firstName=John, lastName=Doe, username=johnDoe#1, membershipType=Platinum, address=Address(street=5thAvenue, city=NewYork, state=NewYork))

Ignoring the property

Ignoring the address fields during serialization (Converting the Java object to JSON). First, let’s put the @JsonIgnore annotation above the address field in the Customer class.

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {

  private String firstName;
  private String lastName;
  private String username;
  private String membershipType;
  
  @JsonIgnore
  private Address address;
}

Now, let’s convert the Customer object to JSON String:

  public static void main(String[] args) throws JsonProcessingException {
    Customer customer = new Customer();
    customer.setFirstName("John");
    customer.setLastName("Doe");
    customer.setUsername("johnDoe#1");
    customer.setMembershipType("premium");
    customer.setAddress(new Address("5th Avenue", "New York", "New York"));

    ObjectMapper objectMapper = new ObjectMapper();
    String customerJson = objectMapper.writeValueAsString(customer);

    System.out.println(customerJson);

  }
Output: {“firstName”:”John”,”lastName”:”Doe”,”username”:”johnDoe#1″,”membershipType”:”premium”}
 
As you can see from the output, the address field was ignored and it’s not part of the JSON message.

Now, let’s leave the annotation and convert the JSON to the Customer object (Deserialization):

  public static void main(String[] args) throws JsonProcessingException {
    String jsonString = "{" +
            "\"firstName\":\"John\"," +
            "\"lastName\":\"Doe\"" +
            ",\"username\":\"johnDoe#1\"," +
            "\"membershipType\":\"Platinum\"," +
            "\"address\":{\"street\":\"5thAvenue\",\"city\":\"NewYork\",\"state\":\"NewYork\"}}";

    ObjectMapper objectMapper = new ObjectMapper();
    Customer customerJson = objectMapper.readValue(jsonString, Customer.class);

    System.out.println(customerJson);

  }
Output: Customer(firstName=John, lastName=Doe, username=johnDoe#1, membershipType=Platinum, address=null)
 
The address field is null, which means it was ignored by Jackson during deserialization.
 
That was all about @JsonIgnore annotation in Java.
 
This tutorial is part of the Java JSON tutorial.
 
Happy coding!

 

Leave a Reply

Your email address will not be published. Required fields are marked *