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:

  1. Download Lombok jar for your Java IDE.
  2. After the download is complete, run the lombok.jar file via your terminal using the command as shown below:
    java -jar lombok.jar

    You should see something like below:

  4. Click on the Install/Update button. You will see Install Successful message on complete.
  5. Next, install lombok library as dependency to your project:
  6. For Gradle:
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
    For Maven:
  7. Now, you can create Getter/Setter class as shown in the example below:
    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;
    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<>();
  9. We can simply use the above getter/setter class in the following manner:
    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.setHobbies(Arrays.asList("Coding", "Eating", "Sleeping", "Singing"));
    Customer(id=null, firstName=Danny, lastName=Man, [email protected], balance=1000, deleted=false, hobbies=[Coding, Eating, Sleeping, Singing])