Using Lombok in Spring Boot
Tags: java, springframework, thymeleaf, lombok
Published: 2020-07-27 12:17:56

#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.](https://dev.to/reytech-lesson/form-submitting-with-spring-boot-validation-5clg)

##Step 1. Download and Install 
1. Please download lombok.jar from https://projectlombok.org/download
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/k6hwx274mmy2u265fq0e.png)
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](https://dev-to-uploads.s3.amazonaws.com/i/04i5s0nq6uvpmnq0iglc.png)
5. And now, your installation is completed and restart Eclipse.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mg4s5hy132rtkkzapttt.png)

##Step 2. Add Lombok dependency in pom.xml
```xml
<dependency>  
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <scope>provided</scope>
</dependency>  
```

##Step 3. Add Lombok annotation in Student.class
This is the code before using lombok.
↓
```java
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;
    }

}
```
This is the code by adding Lombok annotation.

↓
```java
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;
}
```
- @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.