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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:32:30  来源:igfitidea点击:

Lombok: RequiredArgsConstructor not working

javalombok

提问by rozerro

It seems that @RequiredArgsConstructornot 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 @RequiredArgsConstructorannotation.

出于某种原因,它似乎适用于没有定义构造函数而是使用@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 Processorsfrom intellij IDE from File -> 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 和项目语言级别