Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Jackson Mix-In Annotations

DZone's Guide to

Jackson Mix-In Annotations

Take a look at how a Jackson mix-in class or interface, as well as some annotations, can help you with (de)serialization when you don't have source code access to POJOs.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Prior to Jackson 1.2, the only way to serialize or deserialize JSON using Jackson was by using one of the following two methods:

  • Adding annotations to modify the POJO classes
  • Writing custom serializers and deserializers

Now imagine you want to serialize or deserialize a third-party POJO which you don’t have access to its source code. What would you do?

Also, you might want your code clean and open to another JSON library, such as GSON.

What would you do to decouple your code from Jackson annotations?

Jackson mix-in annotations help you resolve these kinds of problems. These annotations are used in a mix-in class or interface but function as if they were directly included in the target class.

In this post, we will look at how to use Jackson mix-in annotations.

Sample Application

Let's create a simple Spring Boot application to understand how Jackson mix-in annotation works.

Say you want to serialize or deserialize a User POJO in a Spring Boot application.

Here is the code of the User POJO.

User.java:

package guru.springframework.blog.domain;

public class User {
    private long id;
    private String name;
    private int age;
    private String gender;
    private String email;
    private String phoneNo;

    public User(long id, String name, int age, String gender, String email, String phoneNo) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.email = email;
        this.phoneNo = phoneNo;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                ", phoneNo=" + phoneNo +
                '}';
    }
}


In the preceding code, User is a typical POJO, but it is not designed to be used with data binding. The User class doesn’t have the default constructor, nor any getter/setter methods.

Let’s assume that you don’t have access to the source code of the User POJO or there is some constraint disallowing you from modifying the existing POJO. In this scenario, you can’t serialize or deserialize a User object through annotations or by defining your own custom serializer and deserializer.

Let us see how mix-in annotations can solve this problem.

The Jackson Mix-in Class

For mix-in annotations, you first need to define a mix-in class or interface.

Let’s define an abstract mix-in class for User. Ensure that the mix-in class has a constructor matching the source POJO.

Use the @JsonCreator annotation on the constructor and the @JsonProperty property to specify all the properties of the POJO.

Here is the code for the UserMixin Jackson mix-in class.

UserMixin.java:

package guru.springframework.blog.mixin;

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

public abstract class UserMixin {
    @JsonCreator
    public UserMixin(@JsonProperty Long id, @JsonProperty String name,
                     @JsonProperty int age, @JsonProperty String gender,
                     @JsonProperty String email, @JsonProperty String phoneNo) {

    }
}


UserMixin is an abstract class where the constructor of the class is annotated with @JsonCreator to tell Jackson in what order to pass fields from a JSON object to the constructor.

Each argument of the constructor is annotated with @JsonProperty to indicate the name of the property to bind to.

After creating the UserMixin class, you must configure the ObjectMapper to use the mix-in for the User POJO, like this:

package guru.springframework.blog.mixin;

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

public abstract class UserMixin {
    @JsonCreator
    public UserMixin(@JsonProperty Long id, @JsonProperty String name,
                     @JsonProperty int age, @JsonProperty String gender,
                     @JsonProperty String email, @JsonProperty String phoneNo) {

    }
}


Here is the complete test code to test the Jackson mix-in.

UserTest.java:

package guru.springframework.blog.domain;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import guru.springframework.blog.mixin.UserMixin;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class UserTest {
    private User user;

    @Before
    public void setUp(){
        user = new User(123,"James",23,"Male",
                "james@gmail.com", "1234567890");
    }
    @After
    public void tearDown(){
        user = null;
    }

    @Test
    public void JacksonMixinAnnotationTest() throws JsonProcessingException{
        ObjectMapper objectMapper = buildMapper();
        objectMapper.addMixIn(User.class, UserMixin.class);
        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
        System.out.println(json);

    }

    private static ObjectMapper buildMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig()
                .getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        return objectMapper;
    }

}


As you can see in the code, ObjectMapper is configured in the buildMapper() method.

In the test method, an ObjectMapper is created and the addMixIn() method is called on it. The addMixIn() method configures the association between the mix-in and target classes to be used during serialization.

Here is the output of the Jackson Mix-in test from IntelliJ:

Jackson Mixin Test Results

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
java ,jackson ,pojo ,serialization and deserialization ,mix-in annotations ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}