Why Lombok @Data + @Builder Breaks No‑Args Constructors and How to Fix It
When Lombok's @Data and @Builder annotations are used together, the generated no‑argument constructor disappears, causing compilation errors; this article explains the root cause, demonstrates the problem with code examples, and provides two reliable solutions using @Tolerate or a combination of @RequiredArgsConstructor and @NoArgsConstructor.
Problem Background
Lombok used with both @Data and @Builder causes a compilation error: the no‑argument constructor generated by @Data disappears, which breaks frameworks that rely on it.
Lombok @Data and @Builder Individual Usage
@Data generates getters, setters and a default no‑argument constructor, simplifying Java code when the Lombok plugin and dependency are installed.
Example entity class with @Data shows automatically generated GET/SET methods and a no‑args constructor.
The compiled class confirms the presence of getters, setters, and the default constructor.
Using @Builder generates a full‑argument constructor but does not create getters/setters on its own.
The compiled class shows the all‑args constructor, while getters/setters are still missing.
When @Data and @Builder are combined, getters/setters are generated but the no‑argument constructor disappears, which is unacceptable for many frameworks.
Attempting to add a manual no‑args constructor still results in a compilation error.
Solution
Method 1
When using @Data and @Builder together, add the @Tolerate annotation to a manually written no‑args constructor so Lombok ignores it during generation.
Method 2
Use a combination of @RequiredArgsConstructor (to generate the all‑args constructor) and @NoArgsConstructor (to generate the no‑args constructor) instead of @Builder.
The compiled result shows both constructors present.
Lombok Internals
Java compilation consists of parsing, annotation processing, analysis, bytecode generation, and class file creation.
Lombok uses JDK 6's JSR 269 Pluggable Annotation Processing API to transform annotations into regular Java code during compilation.
It modifies the abstract syntax tree (AST) to inject new nodes, then the final bytecode is generated.
Custom Annotation Processor Example
To illustrate how Lombok works, a custom @MySetter annotation and its processor are created. The processor uses the tools.jar javac API to manipulate the AST and generate a setter method at compile time.
The processor code is compiled first, then applied to a test class, resulting in a generated setter method in the compiled class.
Conclusion
Although Lombok can generate setters and constructors at compile time, the generated methods are not directly callable in the IDE; Lombok’s plugin mechanism bridges this gap, allowing developers to use its features seamlessly during development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
