What is Lombok in Spring Boot

Posted By : Madhubala Choudhary | 31-Jan-2022

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

The following Lombok constructs:

  • var and val
  • @Getter, @Setter
  • @NoArgsConstructor, @AllArgsConstructor
  • @Data
  • @NotNull

Lombok Dependency

To use Lombok in your project, add the lombok dependency to the Maven POM, like this.

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.8</version>

<scope>provided</scope>

</dependency>

 

@Getter and @Setter

We can use the @Getter and @Setter annotations at both the field or class level to generate getters and setters for private fields.

When we use them at the field level, Lombok generates getters and setters only for the decorated fields.

The code to use the @Getter and @Setter annotations at the field level is this.

package guru.springframework.domain.gettersetter;
import lombok.Getter;
import lombok.Setter;
public class FieldLevelGetterSetterDemo {
  private int userId;
  @Getter @Setter
  private String userName;
  @Getter
  private int userAge;
  
  public FieldLevelGetterSetterDemo(int userAge){
    this.userAge=userAge;
  }

}

This code annotates userName with @Getter and @Setter. The code also annotates userAge with @Getter.

 

When we use the @Getter and @Setter annotations at the class level, Lombok generates getter and setter methods for all the fields.

package guru.springframework.domain.gettersetter;

import lombok.*;

/*

@Getter and @Setter annotations for getter and setter methods

*/

@Getter

@Setter

public class GetterSetterUserDemo {

private int userId;

private String userName;

private int userAge;

}

 

@NoArgsConstructor and @AllArgsConstructor

We can use the @NoArgsConstructor annotation to generate the default constructor that takes no arguments. To generate a constructor with arguments for all the field, use the

@AllArgsConstructor annotation.

The code for demonstrating @NoArgsConstructor and @AllArgsConstructor annotations is this.

package guru.springframework.domain.constructor;

import lombok.*;

/*

@NoArgsConstructor annotation for generating a constructor with no parameters

*/

@NoArgsConstructor

/*

@AllArgsConstructor annotation for generating a constructor

with 1 parameter for each field

*/

@AllArgsConstructor

public class ConstructorUserDemo {

private int userId;

private String userName;

private int userAge;

}

 

@Data

@Data is a convenient annotation that combines the features of the following annotations:

  • @ToString

  • @EqualsAndHashCode

  • @Getter

  • @Setter

  • @RequiredArgsConstructor

This code demonstrates the @Data annotation.

package guru.springframework.domain.data;

import lombok.Builder;

import lombok.Data;

@Data

public class DataUserDemo {

private int userId;

private String userName;

private int userAge;

}

 

 

@NonNull

Lombok generates a null check statement if we annotate the parameters of a method or a constructor with @NonNull.

This code shows the usage of @NonNull.

package guru.springframework.domain.nonnull;

import lombok.AllArgsConstructor;

import lombok.NonNull;

public class NonNullUserDemo {

private int userId;

private String userName;

private int userAge;

/*

@NonNull generate a null-check statement

*/

public NonNullUserDemo(int userId, @NonNull String userName, int userAge) {

this.userId = userId;

this.userName = userName;

this.userAge = userAge;

}

}

The preceding code annotates the userName parameter as @NonNull. Lombok will generate code to check for userName and throw NullPointerException if userName is null.

 

Summary

Lombok is a convenient tool that all Java developers should have in their toolkit. It not only makes you code clutter-free, but also saves a significant amount of development time.

When using Lombok for the first time, you might stumble on how to configure it in your IDE. In IntelliJ, you need to have the IntelliJ Lombok plugin. You also need to enable annotation processing. In IntelliJ, go to File->Settings->Build, Execution,Deployment->Compiler->Annotation Processors. Select the Enable annotation processing checkbox.

 

Also Read : https://springframework.guru/spring-boot-with-lombok-part-1/

 

 

About Author

Author Image
Madhubala Choudhary

Madhubala is an experienced Backend Developer with a wealth of industry experience. She possesses extensive hands-on expertise in Java, Spring Boot, and PostgresSQL technologies. Additionally, She have expertise in API implementations, allowing her to seamlessly integrate different systems and services. She has made significant contributions to various projects, including Pandojo, Tutorx, Musical School, and Croniz Mobile App. Her expertise and dedication have played a crucial role in the success of these projects.

Request for Proposal

Name is required

Comment is required

Sending message..