Lombok: RequiredArgsConstructor not working

后端 未结 5 1148
逝去的感伤
逝去的感伤 2021-01-01 12:49

It seems that @RequiredArgsConstructor not working in the code below. Why is it?

import java.io.Serializable;

import lombok.Data;
import lombok         


        
相关标签:
5条回答
  • 2021-01-01 13:25

    It's also worth noting for future readers that @Data also provides @RequiredArgsConstructor, so using both annotations isn't necessary :)

    0 讨论(0)
  • 2021-01-01 13:37

    According to Documentation, Required arguments are final fields and fields with constraints such as @NonNull.

    You need to make username as @NonNull

    @NonNull private String username;
    

    And you need to make them final too.

    0 讨论(0)
  • 2021-01-01 13:37

    Did you installed Lombok plugin in intellij ?

    If not then

    File -> Settings -> Plusings: Search for Lombok (CodeStream) version.

    Restart the IDE and it should be fixed.

    Double Check:

    • You have Lombok library installed using Maven or Gradle.
    • Enabled Annotation Processors from intellij IDE from File -> Settings: Search for Annotation Processors
    0 讨论(0)
  • 2021-01-01 13:42

    Try changing project/module JDK to 1.8.

    Project Structure -> Project Settings->Project SDK and Project Language Level

    0 讨论(0)
  • 2021-01-01 13:50

    The argument fields for @RequiredArgsConstructor annotation has to be final. So this fix will work:

    private final String username;
    

    The IDE IntelliJ makes the variable grey (inactive status) when final keyword missed, which is very helpful to detect this kind of mistake.

    0 讨论(0)
提交回复
热议问题