@JsonIgnore annotation is part of the Jackson library. This library is the most used one for working with JSON in Java. Thanks to the ObjectMapper class, we can easily convert Java Object to JSON and vice versa.
Also, Jackson offers us many useful annotations to help us serialize and deserialize Java Objects to/from JSON. In this post, we will explore the @JsonIgnore annotation.
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); } }
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); }
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); }