Guide to use Lombok In Spring Boot
Tags: spring, springboot, java
Published: 2021-02-11 09:13:14

[Project Lombok](https://projectlombok.org/), one of the coolest java library which capable of minimizing your code. This is a plugin that we could configure with your editor and build tools. 

Then you would not have to worry about writing getters, setters, and much more boilerplate stuff you have to write in java classes. Here I’ll discuss how we can integrate Lombok with a Spring Boot application and ways to get used from it.

Technologies Going to Use,

- Java 1.8
- Spring Boot: 2.3.4.RELEASE
- Lombok
- Gradle
- Intellij Idea for IDE

Main topics I’m going to discuss here,

## How Lombok Works?

**Lombok is an annotation processor**, which works on compile-time to add codes into your classes. Annotation processing was added since Java 5, Basically, if we use any type of annotation processor, the compiler coming to that level when compiling the class and give the control to that specific processor to process and do what they need to be done to that class.

Let’s think we added @Getter and @Setter annotation from Lombok to any class. While compilation is happening, the compiler gives the Lombok annotation processor a chance to add whatever the things that need to be added inside the class. So Lombok will add the following into the class.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/05zbf7nw029cmar6em4a.png)

## Adding Required Dependencies

I’ll use [Spring Initializr](https://start.spring.io/) to generate a spring boot project with all the dependencies I need for this tutorial. If you are really new to Spring Boot, Please follow our article on [How to Create a Spring Boot Project](https://javatodev.com/how-to-create-a-spring-boot-project/).

Here I’m only using Spring web and Lombok dependency for this tutorial.

If you need to add these libs manually, for Gradle project add the following into your build.gradle dependencies section,

{% gist https://gist.github.com/javatodev/21eb4bf070931216f7a684572240c331 %}

- compileOnly – This Lombok dependency is only used while compiling the code, So we can use compileOnly for Lombok dependency,

- annotationProcessor – Very similar to compileOnly but also guarantees that transitive dependency are not visible for consumer.

If you are using maven based project, use the following dependencies with your project,

{% gist https://gist.github.com/javatodev/3ebf3c361bf3127d2453dd3cfdb5d040 %}

Base project structure will be like,

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pcargupzaiku83q7glix.png)

## Practical Ways to Use Lombok

From here I’ll explain how we can use lombok with common java projects with real examples and codes. Additionally I’ll embed how lombok will change the code on compilation time when necessary.

### Using @Getter and @Setter In Lombok

We use getters and setters to achieve the encapsulations in java. So basically we declare variables/attributes as private and provide public methods to access and update those private variables.

The traditional way for defining getters and setters

{% gist https://gist.github.com/javatodev/8df16dccf147706b684a328cc2fcae3e %}

Lombok way, we can write the same class using Lombok with fewer codes as below,

{% gist https://gist.github.com/javatodev/a4d4266357a0a5b26c26a7c64ebba410 %}

### @Data for Additional Methods

@Data annotation has more power than using Getter and Setter alone since Data annotation comes with the following additional auto-generated methods,

- RequiredArgsConstructor – Generates a constructor with required arguments.
- ToString – Useful to String method.
- EqualsAndHashCode – Hashcode and equals implementation that check all non-transient fields.

Example usage of @Data for a class.

{% gist https://gist.github.com/javatodev/0a0d9690d73e7b626fa4767343b0ff18 %}

## Lombok issue with first char lowerCase and next upperCase

There is a known issue with Lombok, where that Lombok generates wrong getter and setter for variable/attributes which named as first char lowerCase and next char upperCase.

Eg:-

{% gist https://gist.github.com/javatodev/3c935755d0dc1929240f1ff6462d7929 %}

If we generate getter setter manually it will generate as following, and this is the correct way of having a getter and setter,

{% gist https://gist.github.com/javatodev/09117759b1dd7271336a490630ebc948 %}

But with Lombok, It generates as following,

{% gist https://gist.github.com/javatodev/489f829b53c113c560291b320416e964 %}

As you can see **p is capital in both getter and setter, hence the Lombok generated getter and setter is wrong**.

The way Lombok generates a wrong getter and setter could be problematic when you define an API using spring. Since we couldn’t deserialize incoming HttpRequest with include pCode value to Java Object which has pCode. This happens because of the wrong way Lombok generates these Getters and Setters.

This is just a simple issue which we can face while using Lombok for spring boot. Keep that in mind while you define variables/attributes inside a project.

## Lombok @Builder For Easy Builder Pattern In Java

Builder pattern allows us to build a complex object with whatever the available values when we need it. So in a common way we need to introduce Builder methods along with encapsulation to a class. But with Lombok, we can easily enable the builder method to any type of class we need.

{% gist https://gist.github.com/javatodev/2a2839bdf0710947b103c0d89e242914 %}

While we used @Builder, It adds all the classes and methods we should implement in order to introduce a builder. So the generated class for above BuilderTestRequest is like below,

{% gist https://gist.github.com/javatodev/7ad4d3bfd9dcbc93c620c7ab4b908b95 %}

We can use builder enabled class as below, The speciality here is that we can have a BuilderTestRequest with whatever the available values.

{% gist https://gist.github.com/javatodev/a4acc6de9eb8cda777ae8fbe7c339003 %}

Output

{% gist https://gist.github.com/javatodev/6ebc68eff9e643bcf4ed4c8e51541fdc %}

## How to Use Lombok with Spring Boot

The discussed areas in above like @Getter @Setter and @Data are common for any Java based application development. From here I’ll explain how we can use Lombok plugin to add value addition while writing a clean code in Spring Boot project.

### Constructor Based Dependency Injection Using Lombok @RequiredArgsConstructor

Dependency injection, basically the way of providing dependent class objects into the class where it needs in order to perform its operation. So there are few ways of doing the dependency injection.

Constructor based dependency injection is one of them. Here I’ll explain how we can write constructor based dependency injection using Lombok.

Let’s define sample service and a rest controller inside our application calling UserService.java and BaseAPIController.java

{% gist https://gist.github.com/javatodev/fb818e98d8906c74651c0cc0cf5adf37 %}

{% gist https://gist.github.com/javatodev/84f8a22c7d2407706fd63d553edad600 %}

Ok, now we need to inject our UserService.java into the BaseAPIController.java in order to call the method readUser() in UserService.

The UserService dependency can inject using @RequiredArgsConstructor in Lombok as follows.

{% gist https://gist.github.com/javatodev/17d1066daabd495183b951c7676e3442 %}

So after using @RequiredArgsConstructor Lombok will create the following constructor which make constructor based dependency injection inside the BaseAPIController.

{% gist https://gist.github.com/javatodev/e4500215a4631fb90b3ff4d46e503304 %}

## @Slf4j in Lombok For Logging

Slf4J, serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, logback, and log4j. We can use Slf4j as a logging partner easily when using it lombok.extern.slf4j.

Usage with java class,

{% gist https://gist.github.com/javatodev/1145e6de8afa63cefb09e9045cfaa843 %}

The following line will be added from Lombok while the compilation is happening,

Lombok Generated Class

{% gist https://gist.github.com/javatodev/db9119a351f7f3b991ee6dfdfc2f4dc5 %}

Sample usage inside a RestController

{% gist https://gist.github.com/javatodev/9338164a9df26c4a25ccfda41f4b609b %}

## Additional Tools For Lombok with Java

You can use [Json to Java Converter](https://instantwebtools.net/json-to-java) from instantwebtools.net to easily generate java POJO classes with Lombok annotations for your next project.

# Conclusion

All done, I hope now you have a good idea about how to use Lombok In Spring Boot and any Java-based application development. Comment your ideas or issues you are facing while using Lombok in your project.

If you are looking for spring boot based practical application development tutorials, just check our [article series](https://javatodev.com/list-of-articles/) and comment on whatever the new things you need to see in our website.

You can find source codes for this tutorial from our [Github](https://github.com/javatodev/tutorials/tree/main/spring-boot-lombok-project).