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:
  3. 
    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:
  8. 
    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<>();
    }
    
  9. We can simply use the above getter/setter class in the following manner:
  10. 
    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("danny@tb.com").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=danny@tb.com, balance=1000, deleted=false, hobbies=[Coding, Eating, Sleeping, Singing])