In this post, I will show you how to use @Builder annotation to produces complex builder APIs for your classes.@Builder annotation lets you automatically produce the code required to have your class be instantiable.
Let's create a step by step example to demonstrate generating builder APIs for User class.
Project Lombok Maven
- Create a simple maven project using - How to Create a Simple Maven Project in Eclipse article.
- Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
Follow below steps to install Lombok in eclipse for Windows:
- Downloaded jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
- Execute command in terminal: java -jar lombok.jar
- This command will open window as shown in the picture below, install and quit the installer and restart eclipse.
With Project Lombok
With Project Lombok, we have an easy way to create the Builder object by just using @Builder annotation as below:
package net.javaguides.lombok.builder;
import lombok.Builder;
import lombok.ToString;
@Builder
@ToString
public class User {
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
}
Testing
As you installed Lombok plugin in eclipse so we can test Project Lombok annotations:
package net.javaguides.lombok.builder;
import net.javaguides.lombok.builder.User.UserBuilder;
public class LombokBuilderTest {
public static void main(String[] args) {
// create builder class and add parameters to it.
UserBuilder builder = new UserBuilder();
builder.id(100 L);
builder.firstName("Ramesh");
builder.lastName("Fadatare");
builder.age(28);
builder.gender("Male");
builder.build();
System.out.println();
System.out.println(builder.toString());
System.out.println();
}
}
Generated UserBuilder Class
Using @Builder annotation, when generating the class, a new Builder class will be added. With the above example, if you check the User class in folder /target/classes/net/javaguides/lombok, you will see the content as below:
package net.javaguides.lombok.builder;
public class User
{
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public static class UserBuilder
{
private long id;
private String firstName;
private String lastName;
private int age;
private String gender;
public String toString()
{
return "User.UserBuilder(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
}
public User build()
{
return new User(this.id, this.firstName, this.lastName, this.age, this.gender);
}
public UserBuilder gender(String gender)
{
this.gender = gender;return this;
}
public UserBuilder age(int age)
{
this.age = age;return this;
}
public UserBuilder lastName(String lastName)
{
this.lastName = lastName;return this;
}
public UserBuilder firstName(String firstName)
{
this.firstName = firstName;return this;
}
public UserBuilder id(long id)
{
this.id = id;return this;
}
}
User(long id, String firstName, String lastName, int age, String gender)
{
this.id = id;this.firstName = firstName;this.lastName = lastName;this.age = age;this.gender = gender;
}
public static UserBuilder builder()
{
return new UserBuilder();
}
public String toString()
{
return "User(id=" + this.id + ", firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ", gender=" + this.gender + ")";
}
}
Reference
GitHub Repository
You can view the source code of this article on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorial
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course