How to use Builder Pattern with Jackson for JSON

Celal Kartal
2 min readFeb 18, 2021

--

In a POJO class, GET methods are used for response and SET methods are used for request.

So I mean, when converting JSON to Object, SET methods are used, but when converting Object to JSON, GET methods are used.

So when we want to ignore some JSON fields when converting to Object, we will do some operations (such as ignore null values or ignore whole a field…) via SET methods…The opposite is valid for GET methods.

For example;

The following code are used for Student information. Because we want to ignore number value which comes from request, we used JsonIgnore on setNumber method which is defined in Builder class. Also because we want to ignore age value which goes to response, we used JsonIgnore on getAge method which is defined in Student class.

package com.celalkartal.medium;import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
/**
*
* @author <a href="https://tr.linkedin.com/in/celal-kartal-11434018">Celal
* KARTAL</a>
* @date Feb 18, 2021
*/
@JsonDeserialize(builder = Student.Builder.class)
public class Student {
private String name;
private String surname;
private int age;
private int number;
public Student(Builder builder) {
this.name = builder.name;
this.surname = builder.surname;
this.age = builder.age;
this.number = builder.number;
}
public String getName() {
return name;
}
@JsonInclude(Include.NON_NULL)
public String getSurname() {
return surname;
}
@JsonIgnore
public int getAge() {
return age;
}
public int getNumber() {
return number;
}
@JsonPOJOBuilder(buildMethodName = "buildStudent", withPrefix = "set")
public static class Builder {
String name;
String surname;
private int age;
private int number;
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setSurname(String surname) {
this.surname = surname;
return this;
}
public Builder setAge(int age) {
this.age = age;
return this;
}
@JsonIgnore
public Builder setNumber(int number) {
this.number = number;
return this;
}
public Student buildStudent() {
return new Student(this);
}
}
}

Default buildMethodName value is “build”, but we changed as “buildStudent” and default withPrefix value is “with”, but we changed as “set”.

@JsonPOJOBuilder(buildMethodName = "buildStudent", withPrefix = "set")

So, your field methods which are for editing/assignment should start with set like setName method.

public Builder setName(String name) {
this.name = name;
return this;
}

How does Jackson understand that Builder class will be used for deserialization( or for set methods)?

@JsonDeserialize(builder = Student.Builder.class)

…And for testing…

@PostMapping(value = "/builder")
public Student builderPattern(@RequestBody Student requestSudent) {
Student s = new Student.Builder().setName(requestSudent.getName())
.setSurname(requestSudent.getSurname())
.setAge(30)
.setNumber(40)
.buildStudent();
return s;
}

--

--

Celal Kartal
Celal Kartal

Written by Celal Kartal

Computer Engineer | Software Developer

No responses yet