Common Jackson Annotations – A Complete Guide

Jackson provides a set of powerful annotations that help customize how Java objects are serialized into JSON and deserialized back into Java objects. These annotations allow fine-grained control over the mapping behavior without modifying the core logic of your application.

Common Jackson Annotations

@JsonProperty

  • Purpose: Maps a Java field or getter/setter to a specific JSON property name during serialization and deserialization.
  • Useful when the JSON property name does not match the Java field name.
Example:
import com.fasterxml.jackson.annotation.JsonProperty;

public class Employee {
    @JsonProperty("full_name")
    private String name;
    private int age;

    public Employee() { }

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and Setters
}

👉 In this case, the JSON property will be "full_name" instead of "name".

Serialization Example:
ObjectMapper objectMapper = new ObjectMapper();
Employee emp = new Employee("Ashish Kumar", 30);

String jsonString = objectMapper.writeValueAsString(emp);
System.out.println(jsonString);

Output:

{"full_name":"Ashish Kumar","age":30}

@JsonIgnore

  • Purpose: Prevents a specific field from being serialized (Java to JSON) or deserialized (JSON to Java).
  • Useful for sensitive data (like passwords) or unnecessary properties.
Example:
import com.fasterxml.jackson.annotation.JsonIgnore;

public class Employee {
    private String name;
    private int age;

    @JsonIgnore
    private String password;

    public Employee() { }

    public Employee(String name, int age, String password) {
        this.name = name;
        this.age = age;
        this.password = password;
    }

    // Getters and Setters
}

Serialization Example:

ObjectMapper objectMapper = new ObjectMapper();
Employee emp = new Employee("Ashish Kumar", 30, "secure123");

String jsonString = objectMapper.writeValueAsString(emp);
System.out.println(jsonString);

👉 The password field will be excluded from the JSON output and will not be read during deserialization.

Output:

{"name":"Ashish Kumar","age":30}

@JsonInclude

  • Purpose: Controls the inclusion of properties during serialization based on certain conditions (e.g., exclude null values).
  • Example usage: Exclude null fields to reduce output size.
Example:
import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Employee {
    private String name;
    private Integer age;
    private String department;

    public Employee() { }

    public Employee(String name, Integer age, String department) {
        this.name = name;
        this.age = age;
        this.department = department;
    }

    // Getters and Setters
}
Serialization Example:
ObjectMapper objectMapper = new ObjectMapper();
Employee emp = new Employee("Ashish Kumar", 30, null);

String jsonString = objectMapper.writeValueAsString(emp);
System.out.println(jsonString);

👉 Fields with null values (like department if not set) will be excluded from the serialized JSON.

Output:

{"name":"Ashish Kumar","age":30}

✅ Additional Useful Annotations (Worth Mentioning)

@JsonIgnoreProperties

  • The @JsonIgnoreProperties annotation is used to ignore multiple unknown properties during deserialization.
  • By default, if the incoming JSON contains fields that do not match any properties in the target Java class, Jackson throws an exception (UnrecognizedPropertyException).
  • Adding @JsonIgnoreProperties(ignoreUnknown = true) prevents this exception by simply ignoring any unknown fields.

➤ Why Is It Useful?

  • Useful when the JSON comes from an external source (like a third-party API) that may include additional fields your Java class does not care about.
  • Avoids tightly coupling your Java model to every field in the JSON.

Example:

JSON Input:

{
    "name": "Ashish Kumar",
    "age": 30,
    "extra": "this field is not mapped in Java class"
}
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee {
    private String name;
    private int age;

    // Getters and Setters
}

Behavior:

  • The extra field will be ignored during deserialization.
  • No exception will be thrown, and only name and age will be mapped.

@JsonCreator and @JsonProperty Combination

  • These annotations are used when deserializing into immutable objects or when the target class does not have a default (no-argument) constructor.
  • @JsonCreator: Marks the constructor to be used for deserialization.
  • @JsonProperty: Specifies how the constructor arguments map to JSON properties.

➤ Why Is It Useful?

  • Ensures proper deserialization when using final fields or classes designed to be immutable.
  • Promotes safe object construction without relying on setters.

JSON Input:

{
    "name": "Ashish Kumar",
    "age": 30
}

Java Class:

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

public class Employee {
    private final String name;
    private final int age;

    @JsonCreator
    public Employee(@JsonProperty("name") String name, @JsonProperty("age") int age) {
        this.name = name;
        this.age = age;
    }

    // Getters
    public String getName() { return name; }
    public int getAge() { return age; }
}
Behavior:
  • Jackson uses the annotated constructor to instantiate the object.
  • No need for a default constructor or setters.
  • Allows fully immutable objects while still supporting deserialization.

    ✅ Conclusion

    • @JsonProperty → Maps a Java field to a specific JSON property name.
    • @JsonIgnore → Prevents a field from being serialized or deserialized.
    • @JsonInclude → Controls inclusion of properties during serialization (e.g., omit nulls).

    These annotations remain stable and correct in the latest Jackson versions (e.g., 2.15.x) and Java versions (e.g., Java 17 or later).

    • @JsonIgnoreProperties(ignoreUnknown = true) helps prevent errors caused by unexpected fields in JSON.
    • @JsonCreator with @JsonProperty enables deserialization into immutable objects by mapping constructor arguments directly from JSON properties.

    Java developer with 9+ years of IT experience, sharing tutorials and tips to help learners master Java programming.

    Leave a Reply

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