Java Jackson @JsonGetter

1. Overview

Jackson is an essential tool in the Java ecosystem, providing capabilities to serialize Java objects into JSON and vice versa. One of its features is the @JsonGetter annotation, which customizes the output name of a getter method during serialization.

Annotation Overview

The @JsonGetter annotation in Jackson is employed to signify a method as a getter method for a specific property. Additionally, you can specify a custom name for the property in the serialized JSON, which may differ from the actual Java property name.

2. Development Steps

1. Establish a new Maven project.

2. Incorporate the Jackson dependencies.

3. Construct the Student class, applying the @JsonGetter annotation.

4. Develop a class for serialization.

5. Implement a main method for demonstration.

3. Create a Maven Project

There are different ways to create a simple Maven project:

Create a Simple Maven Project using the Command Line Interface

Create a Simple Maven Project using  Eclipse IDE

Create a Simple Maven Project using  IntelliJ IDEA

4. Maven Dependencies

Open the pom.xml file, and add the following Jackson data binding dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency>

5. Code Program

Within our Student Management System:
// Student.java
import com.fasterxml.jackson.annotation.JsonGetter;
public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @JsonGetter("studentName")
    public String retrieveName() {
        return name;
    }
    public int getAge() {
        return age;
    }
}
// StudentSerializer.java
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class StudentSerializer {
    public static String serialize(Student student) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(student);
    }
}
// MainClass.java
public class MainClass {
    public static void main(String[] args) {
        Student student = new Student("John Doe", 20);
        try {
            String json = StudentSerializer.serialize(student);
            System.out.println("Serialized JSON: " + json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}

Output:

Serialized JSON: {"studentName":"John Doe","age":20}

Code Explanation:

The Student class includes a custom getter method named retrieveName()

By using the @JsonGetter annotation and specifying "studentName" as its value, the serialized output will have "studentName" as the property name instead of a default derived from the getter method name. The StudentSerializer class manages the object-to-JSON conversion. 

In MainClass, we instantiate a student object and serialize it. The resulting JSON confirms that the custom name provided by @JsonGetter is utilized.

6. Conclusion

Jackson's @JsonGetter offers developers a straightforward method to customize the naming of properties in the serialized JSON. This flexibility ensures that the output JSON can be tailored to adhere to specific naming conventions or requirements, irrespective of the method names in the Java class.

Comments