Using JsonbCreator annotation

In this post under JSONB, I will explain the purpose of JsonbCreator annotation with an example.

Whenever we deserialize a json data to java object, it is required that the Class of the object should have default/no argument constructor, so that JSONB runtime can create an instance of the class with the help of the constructor and set the properties.

As a result even if add a custom constructor or factory method that creates the instance, JSONB runtime doesn’t consider them.

JsonbCreator annotation solves the above problem.

If we annotate a custom constructor or factory method with the annotation, then from that point onwards JSONB runtime will use the custom constructor or factory method to create instances.

If custom constructor or factory method has parameters then they must be annotated with JsonbProperty annotation.

If the class has multiple custom constructor or factory methods only one of them has to be annotated with JsonbCreator annotation.

Below is an example


1  import javax.json.bind.annotation.JsonbCreator;
2  import javax.json.bind.annotation.JsonbProperty;
4  public class Book {
5   private String title;
6   private String author;
7   private int price;
9   public Book() {
11  }
13  @JsonbCreator
14  public Book(@JsonbProperty("title") String title, @JsonbProperty("author") String author, @JsonbProperty("price") int price) {
15      System.out.println("Custom Constructor");
16      this.title = title;
17 = author;
18      this.price = price;
19  }
21  public String getTitle() {
22      return title;
23  }
24  public void setTitle(String title) {
25      this.title = title;
26  }
27  public String getAuthor() {
28      return author;
29  }
30  public void setAuthor(String author) {
31 = author;
32  }
33  public int getPrice() {
34      return price;
35  }
36  public void setPrice(int price) {
37      this.price = price;
38  }
40  @Override
41  public String toString() {
42      return this.title + ":" + author + ":" + price;
43  }
44 }

At line 14 to 19, we have created a custom constructor and at line 13 we annotated it with JsonbCreator. The parameters of the custom constructor are also annotated with JsonbProperty.

Now when the object’s json format is deserialized, the custom constructor is used to create the instance.

Below is the main class code

Main Code

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;

public class JsonbDemo12 {
    public static void main(String[] args) {
        JsonbConfig jsonbConfig = new JsonbConfig();
        Jsonb jsonb = JsonbBuilder.create(jsonbConfig);
        Book book1 = new Book();
        String result = jsonb.toJson(book1);
        Book book2 = jsonb.fromJson(result, Book.class);


“author”: “Author1”,
“price”: 1000,
“title”: “Title1”
Custom Constructor


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s