Setup Lombok with Spring Boot

 Lombok is a Java library that takes care of generating lot of this boiler plate code for the developers.

Lombok works by plugging into your build process and hooking into Java byte-code generation stage, to add required code based on your specifications. Lombok uses annotations to identify which piece of code needs to be auto generated.

Adding Lombok to any project is very simple. You simply have to include the  Lombok package in your build path. 

If you are using Spring boot, Lombok version will be resolved for you by Spring.

Maven pom.xml

    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.3.12.RELEASE</version>
        <relativepath>
    </relativepath></parent>

    <dependencies>
        <!--https://mvnrepository.com/artifact/org.projectlombok/lombok-->
        <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <scope>provided</scope>
        </dependency>
    </dependencies>

Lombok Sample Person.java


@Data
public class Person {
    private String name;
    private int age;
}

Unit Test PersonTest.java


class PersonTest {

    @Test
    public void data_annotation() {
        Person person = new Person();
        person.setName("David Hoff");
        person.setAge(25);

        Assertions.assertEquals("David Hoff", person.getName());
        Assertions.assertEquals(25, person.getAge());
    }
}

The entire sample code can be found on Github

0 comments:

Post a Comment