Project Lombok Implementation With Spring Boot

Posted By : Avnish Yadav | 26-Feb-2021

Spring boot

Loading...

Project Lombok could be a Java library tool that generates code for minimizing boilerplate code. The library replaces boilerplate code with easy-to-use annotations.

You can remove code clutters, like getters and setters method, constructor, hashcode, equals, and toString method, and so on by adding some annotations.

This is half one of the Spring Boot with Lombok post. In this half, we’ll discuss the subsequent Lombok constructs:

@Getter, @Setter
@NoArgsConstructor, @AllArgsConstructor
@Data
@NotNull

Lombok Dependency

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

PROJECT LOMBOK DEMO

I am going to create a new user object with a bunch of fields.

package com.therealdanvega;

import java.util.Date;

public class User {

    private String first;
    private String middle;
    private String last;
    private String email;
    private Date dob;
    private String foo;
    private String bar;
    private String a,b,c;

}

That looks sort of a pretty clean class, however we tend to be not done yet. Currently, we want to form a getter & setter for every of these properties. we tend to conjointly would most likely wish to feature a toString, equals and hashCode method to our class.

package com.therealdanvega;

import java.util.Date;

public class User {

    private String first;
    private String middle;
    private String last;
    private String email;
    private Date dob;
    private String foo;
    private String bar;
    private String a,b,c;

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getMiddle() {
        return middle;
    }

    public void setMiddle(String middle) {
        this.middle = middle;
    }

    public String getLast() {
        return last;
    }

    public void setLast(String last) {
        this.last = last;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public String getFoo() {
        return foo;
    }

    public void setFoo(String foo) {
        this.foo = foo;
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() {
        return b;
    }

    public void setB(String b) {
        this.b = b;
    }

    public String getC() {
        return c;
    }

    public void setC(String c) {
        this.c = c;
    }

    @Override
    public String toString() {
        return "User{" +
                "first='" + first + '\'' +
                ", last='" + last + '\'' +
                ", email='" + email + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (!first.equals(user.first)) return false;
        if (middle != null ? !middle.equals(user.middle) : user.middle != null) return false;
        if (!last.equals(user.last)) return false;
        if (!email.equals(user.email)) return false;
        if (!dob.equals(user.dob)) return false;
        if (foo != null ? !foo.equals(user.foo) : user.foo != null) return false;
        if (bar != null ? !bar.equals(user.bar) : user.bar != null) return false;
        if (a != null ? !a.equals(user.a) : user.a != null) return false;
        if (b != null ? !b.equals(user.b) : user.b != null) return false;
        return c != null ? c.equals(user.c) : user.c == null;
    }

    @Override
    public int hashCode() {
        int result = first.hashCode();
        result = 31 * result + (middle != null ? middle.hashCode() : 0);
        result = 31 * result + last.hashCode();
        result = 31 * result + email.hashCode();
        result = 31 * result + dob.hashCode();
        result = 31 * result + (foo != null ? foo.hashCode() : 0);
        result = 31 * result + (bar != null ? bar.hashCode() : 0);
        result = 31 * result + (a != null ? a.hashCode() : 0);
        result = 31 * result + (b != null ? b.hashCode() : 0);
        result = 31 * result + (c != null ? c.hashCode() : 0);
        return result;
    }
}

Now our class is correct however that's an entire bunch of unwanted code. to not mention what a maintenance nightmare this is often. Each single time this class changes we'd like to update the getters, setters, and methods by generating them over or maybe worse, really writing code.

AFTER PROJECT LOMBOK

Now that we've Lombok in our application we are able to cut back this noise. simply add that @Data annotation to your class. this may produce a getter and setter for every property outlined in your class. it'll conjointly produce a to string, equals and hashcode method.

package com.therealdanvega;

import lombok.Data;
import java.util.Date;

@Data
public class User {

    private String first;
    private String middle;
    private String last;
    private String email;
    private Date dob;
    private String foo;
    private String bar;
    private String a,b,c;

}


The great factor regarding Lombok is that it integrates well with the IDE. I'm using IntelliJ however this may add Eclipse additionally. If you look over at your project structure you'll be able to see that every one of those strategies currently exist while not us having to write them.


We provide end-to-end ERP development services to enhance business productivity and solve complex business problems. Our custom ERP application development services enable enterprises to streamline their inbound/outbound operations with advanced problem-solving capabilities. For more information, contact us at [email protected].