How to use lombok.Data annotation in a Spring Boot application?

I have used @Data annotation in my POJO Class but the getters and setters are not generated. IDE which I am using is sts(Spring Tool Suite)

//User POJO Class
import lombok.Data;

@Data
public class UserVo {

    private String name;
    private String userName;
    private String email;
    private String mobile;
    private String password;
}
<!-- pom.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aptitest</groupId>
    <artifactId>wt-online-test-backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>wt-online-test-backend</name>
    <description>Online Aptitude Test</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Solution 1:

The problem here is likely related to your ability to check if getters/setters were actually generated.

  1. If your IDE tells you getters/setters not generated - the IDE might be wrong. It may not pick that getters/setters were generated; For your IDE - make sure you have relevant plugins for that. This is most likely your issue as you are mentioning STS. Try one of links relevant to your case:

    • https://projectlombok.org/setup/eclipse
    • https://projectlombok.org/setup/intellij
  2. Use IDE-independent Maven build to make sure that Lombok generates what it is supposed to.

Solution 2:

For Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

For Gradle:

developmentOnly 'org.springframework.boot:spring-boot-devtools'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

Solution 3:

Lombok is an annotation processor - it has full access to the generated source tree. While annotation processors usually generate new source files, Lombok modifies existing ones by adding new fields or methods.

There are a lot of annotations provided by Lombok. (See full List)

To answer the question: Lombok annotations do not generate the code in development time. It happens only when the Java compiler generates an Abstract Source Tree. So don't expect the code to magically change whenever you add an annotation.

But,you need to get them resolved in your specific IDE so that all dependencies and imports are correctly added. Given below are the ways to resolve Lombok annotations in your preferred IDE. You can also go for a Maven project and resolve these as well (Project Lombok Maven repository).

IntelliJ Idea

1) Enable Annotation Processing

File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> (Tick the checkbox as per the image given below)

Enable Annotation Processor

2) Install/Update Lombok Plugin (see this)

File -> Settings -> Plugins -> Search for Lombok Plugin -> Update or Install

Eclipse

Follow these steps in this link.