How To Use Lombok In Java Programming

Use lombok in Java programming for toString(), equals(), hashCode, getters, setters, builder, constructors

Add lombok dependencies into build.gradle

dependencies {
    //other dependencies...

    // https://projectlombok.org
    // compileOnly 'org.projectlombok:lombok:1.18.8'
    // annotationProcessor 'org.projectlombok:lombok:1.18.8'
    compileOnly "org.projectlombok:lombok:${lombokVersion}"
    testCompileOnly "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
    testAnnotationProcessor "org.projectlombok:lombok:${lombokVersion}"

    //more dependencies...
}

Java POJO with Lombok annotation

import java.util.List;
 
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * Test input
 */
@Data //@ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor
@Builder(toBuilder = true) //static builder and new instance builder
@NoArgsConstructor(access = AccessLevel.PUBLIC) //this is also for Jackson ObjectMapper to do serialization and deserialization
@AllArgsConstructor(access = AccessLevel.PUBLIC) //this is also for Jackson ObjectMapper to do serialization and deserialization
public class TestInput {
    @SuppressWarnings("checkstyle:MemberName") //this is just suppress style check not to use "m_"
    private List<String> hostNames;
    @SuppressWarnings("checkstyle:MemberName") //this is just suppress style check not to use "m_"
    private String serviceName;
}

Use POJO

import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
//more import...
 
public class MainClass {
    public static void main (String[] args) {
        try {
 
            //requestBody is a JsonNode (String works too)
            /*
            {
                "serviceName":"elastic",
                "hostNames":
                ["abc.aws.com"]
            }
            */
 
            //Use Jackson ObjectMapper to do deserialization
            final TestInput testInput = new ObjectMapper().treeToValue(requestBody, TestInput.class);
 
            // lombok will add getters for you
            System.out.println(testInput.getServiceName());
 
            // lombok will create a pretty toString() for you
            System.out.println(testInput.toString());
 
            // lombok will add setters for you
            testInput.setServiceName("set new service name");
            System.out.println(testInput.toString());
 
            // lombok can create a new instance from a existing instance for you
            // this is especially useful that your new instance has one or small amount of fields changed
            final TestInput newTestInput = testInput.toBuilder().serviceName("build into a new instance").build();
            System.out.println(newTestInput.toString());
 
            // lombok make a default static Builder for you
            final TestInput buildTestInputFromScratch = TestInput.builder().hostNames(Arrays.asList("host1", "host2"))
                    .serviceName("build from scratch").build();
            System.out.println(buildTestInputFromScratch.toString());
 
            // default constructor
            final TestInput testInputNoArgs = new TestInput();
            System.out.println(testInputNoArgs.toString());
 
            // all args constructor
            final TestInput testInputAllArgs = new TestInput(Arrays.asList("host3", "host4"), "all arg constructor");
            System.out.println(testInputAllArgs.toString());
 
        } catch (final JsonProcessingException e) {
            e.printStackTrace();
        }
        //other code...
    }
}

Output

elastic
TestInput(hostNames=[abc.aws.com], serviceName=elastic)
TestInput(hostNames=[abc.aws.com], serviceName=set new service name)
TestInput(hostNames=[abc.aws.com], serviceName=build from another instance)
TestInput(hostNames=[host1, host2], serviceName=build from scratch)
TestInput(hostNames=null, serviceName=null)
TestInput(hostNames=[host3, host4], serviceName=all arg constructor)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License