How to Use Lombok in a Java Spring Boot Project?
Lombok is a Java library that helps to minimize writing of boilerplate code by auto generating getter, setter, equals, hashCode, toString methods, and more by using some annotations. The use of Lombok library in code saves space as well as increases the code readability.
To use Lombok, follow the steps below:
- Download Lombok jar for your Java IDE.
- After the download is complete, run the lombok.jar file via your terminal using the command as shown below:
- Click on the Install/Update button. You will see Install Successful message on complete.
- Next, install lombok library as dependency to your project:
- Now, you can create Getter/Setter class as shown in the example below:
- We can simply use the above getter/setter class in the following manner:
java -jar lombok.jar
You should see something like below:

For Gradle:
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
For Maven:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Builder.Default;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
public class Customer {
private String id;
private String firstName;
private String lastName;
private String email;
private BigDecimal balance;
@ Default
private boolean deleted = false;
@ Default
private List<String> hobbies = new ArrayList<>();
}
import java.math.BigDecimal;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Customer customer = Customer.builder().firstName("Danny").lastName("Man").email("[email protected]").build();
customer.setBalance(BigDecimal.valueOf(1000));
customer.setHobbies(Arrays.asList("Coding", "Eating", "Sleeping", "Singing"));
System.out.println(customer);
}
}
Output:
Customer(id=null, firstName=Danny, lastName=Man, [email protected], balance=1000, deleted=false, hobbies=[Coding, Eating, Sleeping, Singing])