How to use lombok.Data annotation in a Spring Boot application?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding @Data
Annotation in Lombok
Lombok is a popular Java library that helps reduce boilerplate code by providing annotations that automatically generate commonly used methods. The @Data
annotation is one of Lombok's essential features, particularly useful in Spring Boot applications, where POJOs (Plain Old Java Objects) and DTOs (Data Transfer Objects) are frequently employed. This article explores the @Data
annotation and how it can simplify your Spring Boot application development.
What is @Data
Annotation?
The @Data
annotation is a convenience annotation that bundles the features of:
@Getter: Automatically generates getter methods for all fields in a class.@Setter: Automatically generates setter methods for all non-final fields.@RequiredArgsConstructor: Generates a constructor for the final fields marked asrequired.@ToString: Generates atoString()method implementation.@EqualsAndHashCode: Generatesequals(Object other)andhashCode()methods based on fields.
Using @Data
means you can avoid repetitive method development, resulting in cleaner, more maintainable code.
How to Use @Data
in a Spring Boot Application
- Add Lombok DependencyFirst, ensure you have Lombok included in your project's dependencies. If you're using Maven, you can add the following Lombok dependency in your
pom.xmlfile:- Getters and setters for
id,name, andemail. - A
toString()method. hashCode()andequals(Object other)methods.
- Lazy Loading: JPA entities require careful setter and getter control. Consider using
@Getterand@Setterselectively instead of@Dataif you're using necessary JPA-specific features or if there are complex relationships. - JavaBeans Spec Compliance: Lombok-generated methods adhere to JavaBeans standards, ensuring compatibility with various libraries and frameworks.
- Final Fields: The
@Dataannotation generates a constructor for fields marked asfinal, making them immutable once set. If immutability is essential, structure your class design accordingly. - Customizing Generated Methods: You can always override Lombok-generated methods by manually creating them in your class. Lombok will respect any explicitly defined methods and won't overwrite them.
- Multi-line Strings: Using Lombok with data models containing multiline strings is seamless. The generated
toString()method will correctly reflect string representation. - Testing: Unit tests should target logic rather than Lombok-generated methods. Rely on Lombok's correctness and avoid writing tests for generated getters, setters, and other utility methods.
Related reading
- How to use MDC with thread pools?
- How to use Mockito with JUnit 5?
- How to use multiple @RequestMapping annotations in spring?
- How to use null in switch
- How to use Nullable and Nonnull annotations more effectively?
- How to use OpenAPI oneOf property with openapi-generator-maven-plugin when generating Spring code
- How to use Project Reactor's Scheduler with Executor based libraries?
- How to use projections and specifications with spring data jpa?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.