DEV Community

Zar Li Hnin for Rey Tech Inc.

Posted on • Updated on

Using Lombok in Spring Boot

What is Lombok?

  • Lombok is a Java library that can automatically plug into your editor and build tools when defining Java classes.

  • If you write Lombok annotations (e.g. @Getter or @setter), you don’t need to write another Getter or Setter methods again.

  • With one annotation, your class has a fully featured builder.

IDE

In this tutorial, we will use Eclipse.

Source Code

You can refer to the source code below.
https://github.com/ZarLiHninn/Form-Submitting-Lombok-WebJars

Let’s start Lombok

We will show you how to use Lombok in our previous project which is Form Submitting with Spring Boot Validation.

Step 1. Download and Install

  1. Please download lombok.jar from https://projectlombok.org/download Alt Text
  2. Open downloaded lombok.jar.
  3. Specify your Eclipse IDE location by clicking Specify location and then you can see your Eclipse location.
  4. Click Install/Update. Alt Text
  5. And now, your installation is completed and restart Eclipse. Alt Text

Step 2. Add Lombok dependency in pom.xml



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


Enter fullscreen mode Exit fullscreen mode

Step 3. Add Lombok annotation in Student.class

This is the code before using lombok.



package com.reytech.demo.model;

import java.time.LocalDate;
import java.util.List;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;

public class Student {

    @NotEmpty(message = "{validation.name.NotEmpty}")
    @Size(min = 2, max = 50, message = "{validation.name.Size}")
    private String name;

    @NotNull(message = "{validation.age.NotNull}")
    @Positive(message = "{validation.age.Positive}")
    @Max(value = 18, message = "{validation.age.Maximum}")
    private Integer age;

    @NotEmpty(message = "{validation.email.NotEmpty}")
    @Email(message = "{validation.email.Type}")
    private String email;

    @NotEmpty(message = "{validation.subjects.NotEmpty}")
    private List <String> subjects;

    @NotNull(message = "{validation.birthDate.NotNull}")
    @Past(message = "{validation.birthDate.Past}")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthDate;

    @NotEmpty(message = "{validation.gender.NotEmpty}")
    private String gender;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

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

    public LocalDate getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(LocalDate birthDate) {
        this.birthDate = birthDate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public List <String> getSubjects() {
        return subjects;
    }

    public void setSubjects(List <String> subjects) {
        this.subjects = subjects;
    }

}


Enter fullscreen mode Exit fullscreen mode

This is the code by adding Lombok annotation.



package com.reytech.demo.model;

import java.time.LocalDate;
import java.util.List;
import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Student {

    @NotEmpty(message = "{validation.name.NotEmpty}")
    @Size(min = 2, max = 50, message = "{validation.name.Size}")
    private String name;

    @NotNull(message = "{validation.age.NotNull}")
    @Positive(message = "{validation.age.Positive}")
    @Max(value = 18, message = "{validation.age.Maximum}")
    private Integer age;

    @NotEmpty(message = "{validation.email.NotEmpty}")
    @Email(message = "{validation.email.Type}")
    private String email;

    @NotEmpty(message = "{validation.subjects.NotEmpty}")
    private List <String> subjects;

    @NotNull(message = "{validation.birthDate.NotNull}")
    @Past(message = "{validation.birthDate.Past}")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthDate;

    @NotEmpty(message = "{validation.gender.NotEmpty}")
    private String gender;
}


Enter fullscreen mode Exit fullscreen mode
  • @Getter and @setter will generate getters and setters according to each field.
  • And, we will explain the most usable Lombok annotations which are:
Lombok annotation Explanation
@data It is all in one annotation which is equivalent to the combination of @Getter @setter @RequiredArgsConstructor @ToString @EqualsAndHashCode.
@Value It is all in one annotation which is equivalent to the combination of @Getter @FieldDefaults(makeFinal=true, level=AccessLevel.PRIVATE) @AllArgsConstructor @ToString @EqualsAndHashCode.
@NoArgsConstructor It is to generate default constructor with no arguments
@RequiredArgsConstructor It is to generate a constructor with required arguments
@ AllArgsConstructor It is to generate a constructor with all fields respectively
@ToString It is to generate the toString method automatically
@RequiredArgsConstructor It is to generate a constructor with required arguments
@EqualsAndHashCode It is to generate HashCode and equals implementations from the fields of your object.
@builder It is to generate the code automatically using Builder pattern for our class or constructor or method.

Top comments (2)

Collapse
 
denday04 profile image
Andreas Stensig

Nice introduction.
I would suggest also adding the @Value annotation to your list. A lot like @Data, but instead it treats the object as immutable, so no setters, and some restriction on constructors and fields. I use it even more than @Data, to help keep a more clean object API that isn't polluted with unintended getters.

Collapse
 
zarlihninn profile image
Zar Li Hnin

Thank you for your suggestion!