Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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> 
like image 272
Deepanshu Avatar asked Feb 13 '16 09:02

Deepanshu


People also ask

How do I use Lombok annotation in spring boot?

As of IntelliJ version 2020.3, you don't need to configure the IDE to use Lombok anymore. But for the older version, you need to have the IntelliJ Lombok plugin. You also need to enable annotation processing. In IntelliJ, go to File->Settings->Build, Execution, Deployment->Compiler->Annotation Processors.

What does @data annotation of Lombok do?

The @Data annotation does the following work: It generates the getter methods for all the fields. It generates the setter methods for all the non-final fields. It generates the toString() method implementation.

What is the use of @data annotation in Java?

@Data is a convenient shortcut annotation that bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together: In other words, @Data generates all the boilerplate that is normally associated with simple POJOs (Plain Old Java Objects) and beans: getters for all fields, ...

What is use of Lombok in spring boot?

Project Lombok is a Java library tool that generates code for minimizing boilerplate code. The library replaces boilerplate code with easy-to-use annotations.


2 Answers

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.

like image 120
Witold Kaczurba Avatar answered Oct 28 '22 05:10

Witold Kaczurba


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' 
like image 36
Babu Avatar answered Oct 28 '22 04:10

Babu