Spring Boot with Lombok
Posted By : Ritik Jain | 09-Jul-2021
Project Lombok is a Java library tool that generates annotations for minimizing the code used for creating functions like getters and setters methods, constructors, hashcode, equals, and toString methods, and so on. The library replaces these codes with easy-to-use annotations.
To use Lombok in our project, we need to download its maven dependency given below:-
There are many annotations available in Lombok like:-
- @Getter, @Setter
- @NoArgsConstructor, @AllArgsConstructor
@Data annotation is equivalent to combination of Lombok’s @Getter + @Setter + @RequiredArgsConstructor + @ToString + @EqualsAndHashCode.
If we need to declare getter and setter for any model or DTO then we just use @Getter and @Setter annotation.
We can also use @Getter and @Setter annotations at the class level then Lombok generates getter and setter methods for all the fields.
We can use the @NoArgsConstructor annotation to generate the default constructor that takes no arguments. To generate a constructor with arguments for all the fields, use the @AllArgsConstructor annotation.