Java 龙目岛:RequiredArgsConstructor 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37671467/
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
Lombok: RequiredArgsConstructor not working
提问by rozerro
It seems that @RequiredArgsConstructor
not working in the code below. Why is it?
似乎@RequiredArgsConstructor
在下面的代码中不起作用。为什么?
import java.io.Serializable;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@Data
@RequiredArgsConstructor
public class User implements Serializable {
private String username;
/*public User(String username) {
this.username = username;
}*/
private static final long serialVersionUID = 8043545738660721361L;
}
I get the error:
我收到错误:
javax.faces.el.EvaluationException: java.lang.Error: Unresolved compilation problem:
The constructor User(String) is undefined
For some reason seems it does work for other domain class in which no constructor defined but instead used the @RequiredArgsConstructor
annotation.
出于某种原因,它似乎适用于没有定义构造函数而是使用@RequiredArgsConstructor
注释的其他域类。
采纳答案by a3.14_Infinity
According to Documentation, Required arguments are final fields and fields with constraints such as @NonNull.
根据Documentation,必需参数是最终字段和具有约束的字段,例如@NonNull。
You need to make username as @NonNull
您需要将用户名设为@NonNull
@NonNull private String username;
And you need to make them final too.
而且您还需要使它们成为最终版本。
回答by adrianostgard
It's also worth noting for future readers that @Dataalso provides @RequiredArgsConstructor, so using both annotations isn't necessary :)
对于未来的读者,还值得注意的是@Data还提供了@RequiredArgsConstructor,因此不需要同时使用这两个注释:)
回答by HA S
Did you installed Lombok plugin in intellij ?
您是否在 intellij 中安装了 Lombok 插件?
If not then
如果没有那么
File -> Settings -> Plusings: Search for Lombok (CodeStream) version.
File -> Settings -> Plusings: Search for Lombok (CodeStream) version.
Restart the IDE and it should be fixed.
重新启动IDE,它应该被修复。
Double Check:
再检查一遍:
- You have Lombok library installed using Maven or Gradle.
- Enabled
Annotation Processors
from intellij IDE fromFile -> Settings: Search for Annotation Processors
- 您已使用 Maven 或 Gradle 安装了 Lombok 库。
Annotation Processors
从 Intellij IDE启用File -> Settings: Search for Annotation Processors
回答by Harinath
Try changing project/module JDK to 1.8.
尝试将项目/模块 JDK 更改为 1.8。
Project Structure -> Project Settings->Project SDK and Project Language Level
项目结构 -> 项目设置 -> 项目 SDK 和项目语言级别