Generate a Lombok @Builder POJO from JSON
Combine Lombok's @Data with @Builder to produce a fluent builder API for your Java POJOs. Useful for DTOs with many optional fields.
Detailed Explanation
The Builder Pattern with Lombok
Constructors with many parameters become unreadable quickly. Lombok's @Builder generates a fluent builder so you can construct objects field-by-field with named arguments.
Example JSON
{
"id": 100,
"title": "Inbox Zero",
"description": "Reach inbox zero by Friday.",
"priority": "high",
"completed": false
}
Generated Java with @Data + @Builder
After running the converter with Lombok @Data enabled, manually add @Builder:
package com.example.model;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class Task {
private Integer id;
private String title;
private String description;
private String priority;
private Boolean completed;
}
Using the Builder
Task task = Task.builder()
.id(100)
.title("Inbox Zero")
.description("Reach inbox zero by Friday.")
.priority("high")
.completed(false)
.build();
Why Builders Beat Telescoping Constructors
A constructor with five parameters is hard to read at the call site:
new Task(100, "Inbox Zero", "Reach inbox zero by Friday.", "high", false);
A builder is self-documenting:
Task.builder()
.title("Inbox Zero")
.priority("high")
.build();
You can omit fields, and parameter order does not matter — both major wins for readability and refactoring safety.
@Builder.Default for Defaults
If a field should default to a non-null value, mark it with @Builder.Default:
@Builder.Default
private Boolean completed = false;
Builders and Records
Records do not support Lombok @Builder because they are immutable and have a fixed canonical constructor. For records, write a static builder() method by hand or use a code-gen tool like RecordBuilder.
Builders for Deserialization
Jackson can deserialize directly into a builder if you add @JsonDeserialize(builder = Task.TaskBuilder.class) and @JsonPOJOBuilder(withPrefix = "") on the builder. This pattern is especially useful for immutable objects with many optional fields.
Use Case
Form submission DTOs, search query builders, and configuration objects with many optional fields gain enormously from a builder API. Test fixtures also become readable: Order.builder().status("pending").build() vs new Order(null, "pending", 0, null, null).