What is Lombok in java

Posted By : Rahul Chauhan | 25-Nov-2018

In this, we will discuss what is Lombok and how to use it.
Lombok is a small library which is used to reduce the amount of Java code from the project. In the java projects, we have required the Dto and Entity classes in which we define the fields and write the getter, setter, and constructors followed by the fields. for ex: we have an entity Todo:

public class Todo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String summary;
    private String description;

    @Override    
    public int hashCode() {         
        final int prime = 31;         
        int result = 1;         
        result = prime * result + ((description == null) ? 0 : description.hashCode());         
        result = prime * result + ((id == null) ? 0 : id.hashCode());         
        result = prime * result + ((summary == null) ? 0 : summary.hashCode());    
        return result; 
    }    
    public Long getId() {  
        return id;     
    }     
    public void setId(Long id) {   
        this.id = id;     
    }    
    public String getSummary() { 
        return summary;    
    }   
    public void setSummary(String summary) {   
        this.summary = summary;    
    }   
    public String getDescription() {  
        return description;    
    }    
    public void setDescription(String description) {    
        this.description = description;   
    } 
}

to use that entity we need to generate getter and setter but after using Lombok we only required the put the annotation in the entity it will automatically generate the getter and setter internally for that entity and it helps to reduce the line of code and also reduce the effort for creating getter and setter.

 

How to implement Lombok in our project?
Step 1: To implement Lombok in our project add Lombok to your project 
        <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.8</version>
                <scope>provided</scope>
          </dependency>

Step 2: Download the Lombok jar file from the link:
    https://projectlombok.org/download.html
    and paste it in the root folder of sts or eclipse.
Step 3: open jar location in the terminal and run the command "java -jar lombok.jar"  it opens a popup you need to set the path of sts executable file and click the install button. after installing you need to put the annotation @Data it will generate all getter, setter, constructor, toString and hashcode method internally. 

After following the above steps your todo entity is :

@Data
public class Todo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String summary;
    private String description;
}

About Author

Author Image
Rahul Chauhan

Rahul Chauhan is Java Developer having good knowledge of java, have a knowledge of Spring Boot.

Request for Proposal

Name is required

Comment is required

Sending message..