Using Lombok in Spring Boot: Simplifying Your Code

Marcelo Domingues
devdomain
Published in
10 min readJust now

In modern Java development, one of the challenges developers face is writing and maintaining boilerplate code. From getters and setters to constructors and logging, Java applications often require a lot of repetitive code that doesn’t contribute directly to the business logic. This is especially true in frameworks like Spring Boot, where model classes, DTOs (Data Transfer Objects), and service layers can involve a significant amount of boilerplate.

Project Lombok is a Java library that aims to reduce this boilerplate by automatically generating code for commonly used patterns like getters, setters, constructors, and more. By integrating Lombok with Spring Boot, you can significantly simplify your code, making it easier to read, maintain, and write. In this detailed guide, we will explore how to use Lombok in Spring Boot applications, cover its key features, and provide examples of how it can simplify your code.

Reference Image

What is Lombok?

Lombok is a Java library that plugs into your IDE and builds tools to automatically generate boilerplate code at compile-time. This means that developers don’t have to write repetitive code for getters, setters, constructors, or logging — Lombok does it for them.

The library integrates seamlessly with popular IDEs such as IntelliJ IDEA and Eclipse, and provides annotations that instruct the compiler to inject methods, constructors, and other utilities into your classes. These annotations are processed at compile-time, so you won’t see the actual code in your source files, but they will be present in the compiled bytecode.

Lombok is particularly useful in Spring Boot applications, where we often deal with POJOs (Plain Old Java Objects), entities, and service classes that follow the same patterns.

Why Use Lombok in Spring Boot?

Using Lombok in a Spring Boot application offers several benefits:

  • Reduced Boilerplate: Lombok eliminates the need for manually writing getters, setters, constructors, equals, hashCode, and toString methods, reducing the amount of repetitive code.
  • Cleaner Code: With Lombok, your classes are cleaner and more concise, allowing you to focus on the business logic instead of the boilerplate.
  • Improved Readability: By removing unnecessary boilerplate, your code becomes easier to read and understand. This is especially important in large codebases.
  • Faster Development: Lombok reduces the time spent writing and maintaining boilerplate code, allowing developers to focus on solving actual problems.

Setting Up Lombok in a Spring Boot Project

Before we dive into Lombok’s features, let’s set it up in a Spring Boot project. We will use Maven for dependency management, but you can also use Gradle.

Step 1: Adding Lombok Dependency

To start using Lombok, you need to add the Lombok dependency to your project’s pom.xml (for Maven) or build.gradle (for Gradle) file.

For Maven, check this dependency:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

For Gradle:

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

Step 2: Lombok Plugin for IDE

Lombok works by injecting code at compile-time, so your IDE needs to recognize the Lombok annotations. Here’s how to set up Lombok for popular IDEs:

  • IntelliJ IDEA: Go to File > Settings > Plugins and search for Lombok. Install the plugin and restart your IDE.
  • Eclipse: Install the Lombok plugin by downloading the Lombok JAR from the official website and running it. This will set up Lombok in your Eclipse IDE.

Once the plugin is installed, your IDE will recognize Lombok annotations and generate the relevant code at compile time.

Core Lombok Annotations in Spring Boot

Lombok provides a variety of annotations to help simplify your code. Below, we’ll explore some of the most commonly used Lombok annotations in the context of a Spring Boot application.

1. @Getter and @Setter

The @Getter and @Setter annotations automatically generate getter and setter methods for the fields in your class. Without Lombok, you would have to write these methods manually, but with Lombok, a single annotation can replace all of them.

Without Lombok:

public class User {
private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}

With Lombok:

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class User {
private String firstName;
private String lastName;
}

Lombok generates the getFirstName, setFirstName, getLastName, and setLastName methods automatically. This significantly reduces boilerplate code.

You can also control the visibility of these methods:

@Getter(AccessLevel.PUBLIC)
@Setter(AccessLevel.PRIVATE)
public class User {
private String firstName;
private String lastName;
}

In this case, Lombok will generate a public getter but a private setter.

2. @ToString

The @ToString annotation generates a toString() method for your class, which is useful for logging and debugging. Without Lombok, you would have to manually write this method to print the object’s field values.

Without Lombok:

public class User {
private String firstName;
private String lastName;

@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}

With Lombok:

import lombok.ToString;

@ToString
public class User {
private String firstName;
private String lastName;
}

Lombok automatically generates the toString() method, which will output:

User(firstName=John, lastName=Doe)

You can also exclude certain fields from the toString() method if they are sensitive or irrelevant:

@ToString(exclude = "password")
public class User {
private String firstName;
private String lastName;
private String password;
}

3. @EqualsAndHashCode

The @EqualsAndHashCode annotation generates equals() and hashCode() methods for your class. These methods are critical when using objects in collections like Set or when comparing objects.

Without Lombok:

public class User {
private String firstName;
private String lastName;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(firstName, user.firstName) &&
Objects.equals(lastName, user.lastName);
}

@Override
public int hashCode() {
return Objects.hash(firstName, lastName);
}
}

With Lombok:

import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class User {
private String firstName;
private String lastName;
}

Lombok automatically generates the equals() and hashCode() methods based on the fields of the class.

You can also customize which fields are used for equality checks:

@EqualsAndHashCode(of = {"firstName"})
public class User {
private String firstName;
private String lastName;
}

This will make Lombok generate equals() and hashCode() methods that only consider the firstName field.

4. @NoArgsConstructor, @AllArgsConstructor, and @RequiredArgsConstructor

These annotations are used to automatically generate constructors for your class.

  • @NoArgsConstructor: Generates a no-argument constructor.
  • @AllArgsConstructor: Generates a constructor with all fields as parameters.
  • @RequiredArgsConstructor: Generates a constructor with required (final) fields as parameters.

Example: Using @AllArgsConstructor and @NoArgsConstructor:

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class User {
private String firstName;
private String lastName;
}
  • @AllArgsConstructor generates a constructor with firstName and lastName as parameters.
  • @NoArgsConstructor generates a no-argument constructor.

Example: Using @RequiredArgsConstructor:

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class User {
private final String firstName;
private final String lastName;
private int age;
}

In this case, Lombok generates a constructor that takes firstName and lastName (the final fields) as parameters, but not age because it’s not final.

5. @Data

The @Data annotation is a combination of several annotations: @Getter, @Setter, @ToString, @EqualsAndHashCode, and @RequiredArgsConstructor. It’s useful when you want to apply all of these annotations at once to simplify a POJO class.

Without Lombok:

public class User {
private String firstName;
private String lastName;

public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

// Getters, Setters, toString(), equals(), and hashCode() go here...
}

With Lombok:

import lombok.Data;

@Data
public class User {
private String firstName;
private String lastName;
}

The @Data annotation generates all the boilerplate code for getters, setters, toString(), equals(), hashCode(), and a constructor for final fields.

Using Lombok in Spring Boot Components

Now that we’ve explored the basic Lombok annotations, let’s see how they can be applied in a Spring Boot application. In Spring Boot, Lombok can be used to simplify various components like entities, service classes, repositories, and controllers.

1. Simplifying Entities with Lombok

Spring Boot applications often involve entities that map to database tables. These entities typically require getters, setters, constructors, and equals/hashCode methods, which can result in a lot of boilerplate code.

Example: User Entity Without Lombok

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String firstName;
private String lastName;

public User() {}

public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

// Getters, Setters, toString(), equals(), and hashCode()...
}

With Lombok:

import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String firstName;
private String lastName;
}

Using @Data, you eliminate the need to manually write getters, setters, constructors, and equals/hashCode methods, greatly reducing the size of your entity classes.

2. Using Lombok in Service Classes

In Spring Boot, service classes often involve logging and require dependency injection for various components. Lombok can simplify both logging and constructor-based dependency injection.

Example: Service Class Without Lombok

@Service
public class UserService {
private final UserRepository userRepository;
private final EmailService emailService;

private static final Logger logger = LoggerFactory.getLogger(UserService.class);

public UserService(UserRepository userRepository, EmailService emailService) {
this.userRepository = userRepository;
this.emailService = emailService;
}

public void createUser(User user) {
logger.info("Creating user: {}", user);
userRepository.save(user);
emailService.sendWelcomeEmail(user);
}
}

With Lombok:

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserService {
private final UserRepository userRepository;
private final EmailService emailService;

public void createUser(User user) {
log.info("Creating user: {}", user);
userRepository.save(user);
emailService.sendWelcomeEmail(user);
}
}
  • @RequiredArgsConstructor generates a constructor that takes the final fields (userRepository and emailService) as parameters.
  • @Slf4j generates a logger for the class, allowing you to log messages without manually creating a Logger instance.

3. Lombok in Controllers

Controllers in Spring Boot often have constructor-based dependency injection and logging. Lombok can simplify these aspects in controller classes as well.

Example: Controller Without Lombok

@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

public UserController(UserService userService) {
this.userService = userService;
}

@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
logger.info("Received request to create user: {}", user);
userService.createUser(user);
return ResponseEntity.ok(user);
}
}

With Lombok:

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {
private final UserService userService;

@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
log.info("Received request to create user: {}", user);
userService.createUser(user);
return ResponseEntity.ok(user);
}
}
  • @RequiredArgsConstructor generates the constructor that injects the userService dependency.
  • @Slf4j generates a logger instance for logging incoming requests and responses.

Advanced Lombok Features

In addition to the basic features, Lombok provides several advanced annotations that can further simplify Spring Boot applications.

1. @Builder

The @Builder annotation implements the builder pattern for your class, allowing you to create complex objects in a more readable and flexible way. This is especially useful when working with immutable objects or when you have optional fields.

Example: Using @Builder

import lombok.Builder;

@Builder
public class User {
private String firstName;
private String lastName;
private String email;
}

You can now create a User object using a fluent builder syntax:

User user = User.builder()
.firstName("John")
.lastName("Doe")
.email("john.doe@example.com")
.build();

This approach makes object construction more flexible and readable, particularly when dealing with objects that have many optional fields.

2. @Value

The @Value annotation is used for immutable classes. It is similar to @Data, but all fields are marked as private final and no setters are generated. It is useful for creating immutable DTOs.

Example: Using @Value

import lombok.Value;

@Value
public class UserDTO {
private String firstName;
private String lastName;
private String email;
}

In this case:

  • Lombok generates a constructor that takes all fields as parameters.
  • All fields are final and private, making the class immutable.
  • No setters are generated, ensuring that the object cannot be modified after creation.

3. @Singular

The @Singular annotation is used with @Builder to allow for adding elements to collections in a builder pattern. It can be applied to collections like List, Set, and Map.

Example: Using @Singular

import lombok.Builder;
import lombok.Singular;

@Builder
public class Team {
private String name;
@Singular
private List<String> members;
}

Now you can build a Team object and add members to the list one by one:

Team team = Team.builder()
.name("Development Team")
.member("John")
.member("Jane")
.build();

This provides a more flexible and readable way to build objects that contain collections.

Best Practices for Using Lombok in Spring Boot

While Lombok can greatly simplify your code, there are a few best practices to keep in mind when using it in a Spring Boot application:

1. Be Aware of IDE and Build Tool Compatibility

Lombok relies on IDE plugins and build tools (e.g., Maven or Gradle) to process annotations at compile-time. Ensure that your team’s IDEs are properly set up to recognize Lombok annotations, and include Lombok in your project’s dependencies to ensure smooth builds.

2. Use @Data Carefully

While @Data is convenient, it generates both equals() and hashCode() methods based on all fields. If your class has fields that should not be used for equality checks (such as IDs or timestamps), consider using @Getter, @Setter, and @EqualsAndHashCode separately to control which fields are included in the equality check.

3. Understand Generated Code

Lombok simplifies the process of writing boilerplate code, but it’s essential to understand what Lombok generates under the hood. Familiarize yourself with the behavior of annotations like @Data, @Builder, and @EqualsAndHashCode to avoid unexpected behavior in your application.

4. Avoid Overusing Lombok

While Lombok is a great tool for reducing boilerplate code, it’s important not to overuse it. Use Lombok where it provides clear benefits (e.g., for getters, setters, and constructors), but avoid using it in situations where it might obscure the clarity of your code.

Conclusion

Lombok is a powerful tool for simplifying your code in Spring Boot applications. By eliminating boilerplate code for getters, setters, constructors, logging, and more, Lombok enables you to write cleaner, more maintainable code. It seamlessly integrates with Spring Boot and can be used to simplify entities, service classes, controllers, and more.

Through annotations like @Getter, @Setter, @Data, @Builder, @Slf4j, and others, Lombok helps developers focus on business logic instead of repetitive, boilerplate code. However, it’s important to understand how Lombok works under the hood and to use it judiciously to avoid issues in your application’s codebase.

Incorporating Lombok into your Spring Boot projects can lead to cleaner, more efficient development practices, ultimately helping you build more scalable and maintainable applications.

Explore More on Spring and Java Development:

Enhance your skills with our selection of articles:

  • Java Lambda Expressions: Techniques for Advanced Developers (Jun 19, 2024): Dive deep into advanced usage of lambda expressions in Java. Read More
  • Mastering Spring Security: Roles and Privileges (Jun 19, 2024): Learn the essentials of managing roles and privileges in Spring Security. Read More
  • Publishing Your Java Library to Maven Central: A Step-by-Step Tutorial (Mar 25, 2024): A comprehensive guide to making your Java library accessible to millions of developers worldwide. Read More
  • The Art of Unit Testing: Elevate Your Code with Effective Mocks (Mar 13, 2024): A complete guide to using mocks in unit testing, emphasizing the benefits of component isolation. Read More
  • Spring Beans Mastery (Dec 17, 2023): Unlock advanced application development techniques. Read More
  • JSON to Java Mapping (Dec 17, 2023): Streamline your data processing. Read More

References:

  1. Lombok Official Documentation
    Available at: https://projectlombok.org/
  2. Baeldung — Lombok Annotations
    Available at: https://www.baeldung.com/intro-to-project-lombok
  3. DZone — Lombok for Spring Boot
    Available at: https://dzone.com/articles/using-lombok-in-spring-boot-to-avoid-boilerplate-co
  4. Spring Boot Lombok Setup
    Available at: https://www.javadevjournal.com/spring-boot/spring-boot-lombok/
  5. Lombok GitHub Repository
    Available at: https://github.com/projectlombok/lombok

--

--

Marcelo Domingues
devdomain

🚀 Senior Software Engineer | Crafting Code & Words | Empowering Tech Enthusiasts ✨ 📲 LinkedIn: https://www.linkedin.com/in/marcelogdomingues/