How to use Lombok with Spring Boot

Project Lombok is one of those libraries that a lot of developers appreciate. Not only because it saves you a lot of time writing boilerplate code, but you also end up with leaner and cleaner code which is easier and more pleasant to read. If you are considering to introduce Lombok to your project, then you should read about the advantages and disadvantages of using Project Lombok before making big changes to your code.

In this post, we will discuss how to use Lombok inside a Spring boot application.

Enabling Lombok in your IDE

Before you could use Lombok, you will need to add the corresponding Lombok plugin to your IDE. This is because the plugin will be needed in order for the IDE to understand the Lombok annotations and how classes compile into bytecode.

For most IDEs out there, there is a Lombok plugin. Below is a summary of the available plugins for the major IDEs.

IDEPlugin location / Installation resources
IntelliJ based IDEs https://plugins.jetbrains.com/plugin/6317-lombok
Eclipse based https://projectlombok.org/setup/eclipse
https://projectlombok.org/downloads/lombok.jar
Netbeans https://netbeans.org/kb/73/java/annotations-lombok.html

Remember to reboot your IDE in order to make sure that the plugin installation is effective, specially if you are facing any difficulties.

You can check our video on Youtube for instructions on installing the Lombok plugin on IntelliJ.

Adding the Lombok dependency

The next step in this guide is to add the required Lombok dependency to your POM.xml file.

If you are using grade, then you will need to add the following configurations to your build.gradle.

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
 
repositories {
	mavenCentral()
}

dependencies {
	compileOnly 'org.projectlombok:lombok:1.18.10'
	annotationProcessor 'org.projectlombok:lombok:1.18.10'
}

Alternatively, you can also check the Gradle-Lombok plugin here.

Please note that you should always check the maven repository for the latest versions of your plugins. You can find the latest version of Project Lombok here.

If your project is inheriting from the Spring-boot-starter-parent, then you do not need to add the plugin’s version number in your POM, as it is already included in the parent project.

Annotating classes with Lombok annotations

The steps described in the last two sections are basically all you need to get up and running with Lombok in Spring boot. You can test out the configuration by trying it out in some POJO classes.

Take for example the following class.

package com.nullbeans.demo.indicators;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Candlestick {

    private String name;

    private double close;

    private int volume;

}

The class above has three instance variables with the types String, double and int. As you may notice, we also added three Lombok annotations.

  • @Data: This adds getters and setters for each of the instance variables of the class. This behavior is customizable but we will discuss this in a later post.
  • @NoArgsConstructor: This adds a no-arguments constructor to the class.
  • @AllArgsConstructor: This adds a constructor with all the instance variables that exist in the class. The arguments of the constructor have the same order as the order with which the instance variables appear in the class.

Now, you can simply use the class the same way as if you had written those getters, setters and constructors yourself.

public static void main(String[] args) {

Candlestick candlestick = new Candlestick();	// No Args Construction

candlestick.setClose(3.3);
candlestick.setName("abc");
candlestick.setVolume(12345);

Candlestick candlestick1 = new Candlestick("MyName", 3.2, 22);

The Lombok library modifies the compiler’s abstract syntax tree in order for it to compile the Lombok annotations in a way that would allow us to produce class bytecode that contains those constructors, getters and setters that were indicated by the Lombok annotations.

In other words, Lombok makes the compiler understand that the annotation @Data is the same as if we had written a getter and setter for each field, and the @NoArgsConstructor as a no argument constructor, and so on..

The Lombok plugin is required in order for your IDE to understand that the boilerplate code will be generated in order to allow you to use those auto-generated code.

Final notes and summary

As we have seen, Lombok makes it easy to avoid writing boilerplate code which would needlessly inflate the code of simple Java classes. Configuring Lombok is a 5 minute job and the Lombok project has been around for many years with great support from the development community.

So for now, it seems like a worthy addition to any Spring-boot project which will help you keep your code clean and your development effort more efficient. Speaking of clean code..