Lombok @Data reduce boilerplate code for model/data objects in Java

Shambhu Mehta
2 min readJul 17, 2018

--

Avoiding boilerplate code for projection DTOs The code that needs to be written for a DTO can be dramatically simplified using Project Lombok, which provides an @Value annotation

How we can implement Lombok to our project. You can use most popular build tool (Maven ,Gradle) to include Lombok in your project. if you are using STS IDE or IntelliJ IDEA you can include at the time of project creation as well.

Maven: https://projectlombok.org/setup/maven

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>

Using Gradle : https://projectlombok.org/setup/gradle

Implementation with Sample Code : Let’s have an example of below class which we are mostly used in our java POJO . Has only only three field which we are going to use by developer. but it has about 100 lines of code.

I have used with Spring Data JPA you can use in any project by adding Lombok dependencies.

import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entity
public class Task {
@Id@GeneratedValue
private Long id;
private String name;private Boolean completed;Task(){}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Boolean getCompleted() {return completed;}public void setCompleted(Boolean completed) {this.completed = completed;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((completed == null) ? 0 : completed.hashCode());result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic String toString() {return "Task [id=" + id + ", name=" + name + ", completed=" + completed + "]";}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Task other = (Task) obj;if (completed == null) {if (other.completed != null)return false;} else if (!completed.equals(other.completed))return false;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}

Now see once we use Lombok code become 10 line below is code which will serve you the same purpose. just add annotation @Data which is available under package lombok.Data

import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import lombok.Data;@Entity@Datapublic class Task {@Id@GeneratedValueprivate  Long id;private String name;private Boolean completed;Task(){}}

Try using it in your project you love it. happy coding and keep learning share your comment help me to correct myself.

--

--

No responses yet