23

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>
chad luo
  • 109
  • 10
Deepanshu
  • 291
  • 1
  • 3
  • 8
  • Are you using an ide? If so, which one? – Jeff Feb 13 '16 at 17:52
  • 1
    spring-boot-starter-validation is part of spring-boot-starter-web, you don't need them both – David Barda Jul 27 '19 at 18:04
  • `spring-boot-starter-validation` is not part of `spring-boot-starter-web` anymore. They have removed it as from v2.3.0.RELEASE as per the [release notes](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes) – jumping_monkey Jun 22 '20 at 01:37

15 Answers15

10

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'
fyelci
  • 1,399
  • 2
  • 16
  • 27
Babu
  • 111
  • 1
  • 7
  • 4
    What does this code do to answer the question? Please include some more details, thank you. – Carl Poole Mar 07 '17 at 06:39
  • i think, you have to analize for class comparison first ( lombok and data jpa) and then exclude those libraries by using tag in spring-data-jpa dependency – Malakai Mar 07 '17 at 07:07
10

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:

  2. Use IDE-independent Maven build to make sure that Lombok generates what it is supposed to.

Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67
6

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.

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
2
  • Lombok is a very handy tool for minimizing the boilerplate code as well as providing lots of other features such as lazy loading,thread-safety or immutability. This is the reason it becoming very popular among the developer community.
  • If Lombok is in the classpath, it can easily get rid of all the getters & setters methods, class constructors, hashcode and equals methods and many more by just adding a couple of annotations the class.
  • To use Lombok you need to install Lombok in your IDE. Here I am explaining how to do with Eclipse. Install Lombok in Eclipse

1.Download Lombok Jar File

  • Add below dependency in your maven project so that it got downloaded first in your local repository.

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.18</version>
    </dependency>
    

    Now do a mvn clean install command on the newly created project to get this jar downloaded in the local repository.

2.Start Lombok Installation

  • Once the jar downloaded in Local repository, goto the jar location from command prompt and run the following command java -jar lombok-1.16.18.jar and we should be greeted by Lombok installation window provided by lombok.

  • Now click on the “Specify Location” button and locate the eclipse.exe path under eclipse installation folder like this.

  • Now click the “Install/Update” button and we should finish installing Lombok in eclipse.

  • After all the setup done properly you can use all the provide jar of Lombok.

Like:

  • @Data
  • @Getter
  • @Setter
  • @NoArgsConstructor
  • @RequiredArgsConstructor
  • @ToString
  • @EqualsAndHashCode
  • @Delegate
  • @Synchronized
  • @Slf4j
  • @Cleanup

For more details :

Tapan
  • 285
  • 2
  • 8
1

Firstly, you don't need to add @Setter and @Getter. @Data includes these two.
Secondly, you need to add lombok plugin to your IDE.(search for lombok plugin for STS or Enabling Annotation processor for STS). When you do so, you don't really see getters and setters generated in your IDE. This will help in you getting through with compile time errors in your IDE. thats all.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134
1

The Spring Tool Suite IDE is based on eclipse.

From the official Lombok documentation : https://projectlombok.org/download.html

Eclipse and variants — Run lombok.jar as a java app (i.e. doubleclick it, usually) to install. Also add lombok.jar to your project. Supported variants: Springsource Tool Suite, JBoss Developer Studio

If it still does not work, this Stack Overflow discussion might help you :

how to configure lombok in eclipse luna

Community
  • 1
  • 1
PiroXXI
  • 47
  • 1
  • 7
0

You also have to add lombok aus an agent to your IDE. See https://projectlombok.org/download.html for details.

daniel.eichten
  • 2,535
  • 19
  • 26
0

In my case the generated class had lombok generated getters / setters on my entity class. But I was still getting null values being bound for all fields in my entity except for @Id field. I am using gradle not maven.

What fixed the problem for me was to install the Lombok plugin in IntelliJ CE IDE and also to enable the annotation processing for the IDE / project.

After that I am able to persist my entity just fine!

Farrukh Najmi
  • 5,055
  • 3
  • 35
  • 54
0

As you are using spring boot, you can omit the version of lombok (which will then be inherited from the Spring Boot parent POM). Also check that your your IDE is well configured !

1. Spring Boot with Lombok

2. Setting up Lombok with Eclipse and Intellij

3. Using lombok

0

Follow these steps: (100% working)

  1. Download lombok.jar file Link: [[https://projectlombok.org/download][1]]
  2. Double click on that jar file in your local machine. (It will automatically find IDE's .exe files and display)
  3. Use the checkboxes to select IDE that you want to use Lombok.
  4. Click the "Install/Update" button(get few seconds for install). After install click Quit.
  5. Restart your IDE and go to the "Help" tab. Then click on "About (IDE_Name)" Is it show about Lombok on that display, you did it.
  6. Then use @Data annotation in your code. It will work.

Eg:

import lombok.Data;
import lombok.NonNull;

public @Data class Project {

@Id
private @NonNull String id;
private @NonNull String name; 
private @NonNull String description;

}
0

You need to install a lombok jar and add it to build path for eclipse user.

https://howtodoinjava.com/automation/lombok-eclipse-installation-examples/#lombok-eclipse

Alternatively, you can generate getters and setters from source in eclipse and ditch the @Data annotation

0

It is obvious that the problem is project Lombok not working properly. I have the same issue. It perfectly works on IntelliJ, and my instructor told me that IntelliJ has additional plugins that will automatically be installed but vs code and like don't have that feature, so you need manually install project Lombok.so, try by installing the vs code extension related to Lombok.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '22 at 19:45
0

To install Lombok in Eclipse and its variants (like Spring Tool Suite) via the Eclipse plugin installer, you can follow these steps:

Open your IDE and navigate to Help > Install New Software...

x

In the "Work with" field, enter https://projectlombok.org/p2 and check the box next to "Lombok":

f

Complete the installation process and then quit your IDE and start it again (a regular restart may not be sufficient).

Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
  • Be careful when including links to external sites. The link provided appears to be invalid, however it may be regional. You should expand on what the link provides. – moken Feb 15 '23 at 06:57
  • @moken the link should be used inside the eclipse IDE to install the plugin. – Shuvam pargal Feb 15 '23 at 19:30
  • @AndreasViolaris link has no info its just the link that to be used inside eclipse ide to install the plugin required for lombok project – Shuvam pargal Feb 15 '23 at 19:31
  • I understand your point now. The phrase "then go for the link below" has caused confusion for us. To provide greater clarity, I will suggest an edit, and I will also remove the statement regarding the suggested solution's compatibility with "any other IDE", as it is specific to Eclipse and its variants. – Andreas Violaris Feb 15 '23 at 21:54
-1

Study this sample project with Springboot + Lombok. This class use @Data: https://github.com/raulvillalbamedina/exampleApiHateoas/blob/master/src/main/java/com/rvillalba/exampleApiHateoas/entity/Example.java

If your IDE has errors, you need a plugin: "Eclipse and variants Run lombok.jar as a java app (i.e. doubleclick it, usually) to install. Also add lombok.jar to your project. Supported variants: Springsource Tool Suite, JBoss Developer Studio IDEA IntelliJ A plugin developed by Michael Plushnikov adds support for most features."

-5

You need to add @Setter & @Getter annotations to the class, when using Lombok within your pom.xml alternatively use @Data which includes both

Cyber
  • 2,194
  • 4
  • 22
  • 41
V123
  • 21
  • 2