Java Jackson @JacksonInject

1. Overview

Jackson is a comprehensive library in Java, enabling seamless serialization and deserialization between Java objects and JSON representation. Amongst its numerous features, Jackson provides a mechanism to inject values from an external source during deserialization. This is achieved using the @JacksonInject annotation. This post will showcase the usage of this annotation within an Employee Management System example.

@JacksonInject Annotation Overview

The @JacksonInject annotation is primarily used for injecting values from an external source (like a dependency injection framework) during deserialization. The injected values are not part of the incoming JSON but are required to set certain properties of the object.

2. Development Steps

1. Set up a new Maven project.

2. Incorporate the necessary Jackson dependencies.

3. Craft an Employee class and apply the @JacksonInject annotation.

4. Create a main demonstration class.

5. Deserialize the JSON, integrating the external value.

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

For our Employee Management System, contemplate the subsequent classes:
// Employee.java
public class Employee {
    private int id;
    private String name;
    @JacksonInject
    private String companyName;
    // Constructors, Getters, Setters
    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getCompanyName() {
        return companyName;
    }
}

// MainDemo.java
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainDemo {
    public static void main(String[] args) {
        String json = "{\"id\":1,\"name\":\"John Doe\"}";
        ObjectMapper mapper = new ObjectMapper();
        InjectableValues.Std injectableValues = new InjectableValues.Std();
        injectableValues.addValue(String.class, "TechCorp");
        try {
            Employee employee = mapper.readerFor(Employee.class).with(injectableValues).readValue(json);
            System.out.println("Deserialized Employee: " + employee.getName() + ", Company: " + employee.getCompanyName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

Deserialized Employee: John Doe, Company: TechCorp

Code Explanation:

In this demonstration, the Employee class holds an attribute companyName, which isn't present in the JSON string but gets its value injected during deserialization using @JacksonInject. The value is supplied via InjectableValues.Std in the main demonstration class. This approach allows one to inject values, which may be environment-specific or external to the JSON payload, into the deserialized object.

6. Conclusion

The @JacksonInject annotation offers a robust method for integrating external values into objects during deserialization. This is particularly useful for scenarios where certain attributes aren't part of the JSON payload but are essential for the deserialized object. Through our Employee Management System demonstration, we've perceived how this functionality can be effortlessly integrated with Jackson.

Comments