Project Lombok, 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
- Intellij Idea for IDE
Main topics I’m going to discuss here,
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.
I’ll use Spring Initializr 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.
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,
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,
Base project structure will be like,
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.
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
Lombok way, we can write the same class using Lombok with fewer codes as below,
@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.
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.
If we generate getter setter manually it will generate as following, and this is the correct way of having a getter and setter,
But with Lombok, It generates as following,
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.
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.
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,
We can use builder enabled class as below, The speciality here is that we can have a BuilderTestRequest with whatever the available values.
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.
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
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.
So after using @RequiredArgsConstructor Lombok will create the following constructor which make constructor based dependency injection inside the BaseAPIController.
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,
The following line will be added from Lombok while the compilation is happening,
Lombok Generated Class
Sample usage inside a RestController
You can use Json to Java Converter from instantwebtools.net to easily generate java POJO classes with Lombok annotations for your next project.
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 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.