Java 如何在 Spring Boot 应用程序中使用 lombok.Data 注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35378352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use lombok.Data annotation in a Spring Boot application?
提问by Deepanshu
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)
我@Data
在我的 POJO 类中使用了注释,但没有生成 getter 和 setter。我使用的 IDE 是 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>
回答by daniel.eichten
You also have to add lombok aus an agent to your IDE. See https://projectlombok.org/download.htmlfor details.
您还必须将 lombok aus 代理添加到您的 IDE。有关详细信息,请参阅https://projectlombok.org/download.html。
回答by Babu
For Maven:
对于 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'
回答by V123
You need to add @Setter
& @Getter
annotations to the class, when using Lombok within your pom.xml
alternatively use @Data
which includes both
您需要在类中添加@Setter
&@Getter
注释,当在您的pom.xml
交替使用中使用 Lombok 时@Data
,包括两者
回答by pvpkiran
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.
首先,您不需要添加@Setter 和@Getter。@Data 包括这两个。
其次,您需要将 lombok 插件添加到您的 IDE。(搜索 lombok plugin for STS 或 Enabling Annotation processor for STS)。当您这样做时,您实际上看不到 IDE 中生成的 getter 和 setter。这将帮助您解决 IDE 中的编译时错误。就这样。
回答by Raul Villalba
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
使用 Springboot + Lombok 研究这个示例项目。这个类使用@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."
如果您的 IDE 有错误,您需要一个插件:“Eclipse 和变体 Run lombok.jar as a java app(即双击它,通常)进行安装。同时将 lombok.jar 添加到您的项目中。支持的变体:Springsource Tool Suite、JBoss Developer Studio IDEA IntelliJ 由 Michael Plushnikov 开发的插件增加了对大多数功能的支持。”
回答by PiroXXI
The Spring Tool Suite IDE is based on eclipse.
Spring Tool Suite IDE 基于 eclipse。
From the official Lombok documentation : https://projectlombok.org/download.html
来自龙目岛官方文档: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
Eclipse 和变体 — 将 lombok.jar 作为 Java 应用程序运行(即通常双击它)进行安装。还将 lombok.jar 添加到您的项目中。支持的变体:Springsource Tool Suite、JBoss Developer Studio
If it still does not work, this Stack Overflow discussion might help you :
如果它仍然不起作用,这个 Stack Overflow 讨论可能会帮助你:
回答by Witold Kaczurba
The problem here is likely related to your ability to check if getters/setters were actually generated.
这里的问题可能与您检查 getter/setter 是否实际生成的能力有关。
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:
Use IDE-independent Maven build to make sure that Lombok generates what it is supposed to.
如果您的 IDE 告诉您未生成 getter/setter - IDE 可能是错误的。它可能不会选择生成 getter/setter;对于您的 IDE - 确保您有相关的插件。当您提到 STS 时,这很可能是您的问题。尝试与您的案例相关的链接之一:
使用独立于 IDE 的 Maven 构建来确保 Lombok 生成它应该生成的内容。
回答by Farrukh Najmi
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.
在我的例子中,生成的类在我的实体类上有 lombok 生成的 getter/setter。但是我仍然收到为我的实体中的所有字段绑定的空值,@Id 字段除外。我使用的是 gradle 而不是 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.
对我来说解决问题的是在 IntelliJ CE IDE 中安装 Lombok 插件并启用 IDE/项目的注释处理。
After that I am able to persist my entity just fine!
之后,我就可以很好地坚持我的实体!
回答by Keet Sugathadasa
Lombokis 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.
Lombok是一个注释处理器 - 它可以完全访问生成的源代码树。注释处理器通常会生成新的源文件,而 Lombok 通过添加新的字段或方法来修改现有的源文件。
There are a lot of annotations provided by Lombok. (See full List)
Lombok 提供了很多注解。(见完整列表)
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.
回答这个问题:Lombok 注释不会在开发时生成代码。它仅在 Java 编译器生成抽象源树时发生。所以不要指望每次添加注释时代码都会神奇地改变。
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).
但是,您需要在您的特定 IDE 中解决它们,以便正确添加所有依赖项和导入。下面给出了在首选 IDE 中解析 Lombok 注释的方法。您还可以选择 Maven 项目并解决这些问题(Project Lombok Maven 存储库)。
IntelliJ Idea
智能理念
1) Enable Annotation Processing
1) 开启注解处理
File -> Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors -> (Tick the checkbox as per the image given below)
文件 -> 设置 -> 构建、执行、部署 -> 编译器 -> 注释处理器 ->(根据下图勾选复选框)
2) Install/Update Lombok Plugin (see this)
2) 安装/更新 Lombok 插件(见这个)
File -> Settings -> Plugins -> Search for Lombok Plugin -> Update or Install
文件 -> 设置 -> 插件 -> 搜索 Lombok 插件 -> 更新或安装
Eclipse
蚀
Follow these steps in this link.
按照此链接中的这些步骤操作。
回答by Tapan
- 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
- Lombok 是一个非常方便的工具,用于最小化样板代码并提供许多其他功能,例如延迟加载、线程安全或不变性。这就是它在开发者社区中变得非常流行的原因。
- 如果 Lombok 在类路径中,它可以轻松摆脱所有 getter 和 setter 方法、类构造函数、hashcode 和 equals 方法等,只需在类中添加几个注释即可。
- 要使用 Lombok,您需要在 IDE 中安装 Lombok。在这里,我将解释如何使用 Eclipse。 在 Eclipse 中安装 Lombok
1.Download Lombok Jar File
1.下载 Lombok Jar 文件
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 installcommand on the newly created project to get this jar downloaded in the local repository.
在您的 Maven 项目中添加以下依赖项,以便它首先下载到您的本地存储库中。
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency>
现在在新创建的项目上执行mvn clean install命令,将这个 jar 下载到本地存储库中。
2.Start Lombok Installation
2.开始安装龙目岛
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.
在本地存储库中下载 jar 后,从命令提示符转到 jar 位置并运行以下命令 java -jar lombok-1.16.18.jar,我们应该会看到 lombok 提供的 Lombok 安装窗口。
现在单击“指定位置”按钮,然后像这样在 eclipse 安装文件夹下找到 eclipse.exe 路径。
现在单击“安装/更新”按钮,我们应该在 eclipse 中完成 Lombok 的安装。
正确完成所有设置后,您可以使用 Lombok 的所有提供 jar。
Like:
喜欢:
- @Data
- @Getter
- @Setter
- @NoArgsConstructor
- @RequiredArgsConstructor
- @ToString
- @EqualsAndHashCode
- @Delegate
- @Synchronized
- @Slf4j
- @Cleanup
- @数据
- @Getter
- @Setter
- @NoArgsConstructor
- @RequiredArgsConstructor
- @ToString
- @EqualsAndHashCode
- @代表
- @同步
- @Slf4j
- @清理
For more details :
更多细节 :