将 lombok 与 gradle 和 spring-boot 一起使用

新手上路,请多包涵

我正在尝试使用 lombok 构建一个项目,这就是我所拥有的依赖项。

 dependencies {
   compile("org.springframework.boot:spring-boot-starter-thymeleaf")
   compile("org.springframework.social:spring-social-facebook")
   compile("org.springframework.social:spring-social-twitter")
   testCompile("org.springframework.boot:spring-boot-starter-test")
   testCompile("junit:junit")
   compile("org.springframework.boot:spring-boot-devtools")
   compile("org.springframework.boot:spring-boot-starter-data-jpa")
   compile("mysql:mysql-connector-java")
   compileOnly("org.projectlombok:lombok:1.16.10")
}

我能够包含注释,并且在编辑器中包含了 lombok。我什至能够使用 lombok 编译代码并对 lombok 生成的方法进行调用。

这是我的实体:

 @Data
@Entity
@Table(name = "TEA_USER", uniqueConstraints = {
    @UniqueConstraint(columnNames = { "USR_EMAIL" }),
    @UniqueConstraint(columnNames = { "USR_NAME" })
})
public class User {

   @NotNull
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="USR_ID")
   private long id;

   @NotNull
   @Column(name="USR_FNAME")
   private String firstName;

   @NotNull
   @Column(name="USR_LNAME")
   private String lastName;

   @NotNull
   @Min(5)
   @Max(30)
   @Column(name="USR_NAME")
   private String username;

   @Column(name="USR_EMAIL")
   private String email;

   @Min(8)
   @NotNull
   @Column(name="USR_PASSWORD")
   private String password;
}

这是一个编译良好的函数:

 @PostMapping("/registration/register")
public String doRegister (@ModelAttribute @Valid User user, BindingResult result){
    user.getEmail();
    System.out.println(user.getFirstName());
    if (result.hasErrors()) {
         return "register/customRegister";
    }
    this.userRepository.save(user);
    return "register/customRegistered";
}

但是当我运行 bootRun 并尝试访问功能时,这是我得到的异常:

 org.springframework.beans.NotReadablePropertyException: Invalid property 'firstName' of bean class [com.lucasfrossard.entities.User]: Bean property 'firstName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

但是,如果我手动包含 setter 和 getter,则效果很好。我不知道发生了什么以及如何解决它。任何想法?

原文由 Lucas 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 808
1 个回答

使用最新的 Lombok 1.18 很简单。 io.franzbecker.gradle-lombok 不需要插件。我的项目中的示例依赖项:

 dependencies {
    implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
    implementation "org.springframework.social:spring-social-facebook"
    implementation "org.springframework.social:spring-social-twitter"
    implementation "org.springframework.boot:spring-boot-starter-data-jpa"

    testImplementation "org.springframework.boot:spring-boot-starter-test"

    runtimeClasspath "org.springframework.boot:spring-boot-devtools"
    runtime "mysql:mysql-connector-java"

    // https://projectlombok.org
    compileOnly 'org.projectlombok:lombok:1.18.4'
    annotationProcessor 'org.projectlombok:lombok:1.18.4'
}

其他建议:

  1. testCompile("junit:junit") 不是必需的,因为 spring-boot-starter-test “Starter” 包含 JUnit
  2. 使用 implementationapi 配置而不是 compile (自 Gradle 3.4 起)。 我如何选择合适的?
  3. 不要使用 compile 配置 devtoolsDeveloper Tools 指南中的提示:

在 Maven 中将依赖项标记为可选或在 Gradle 中使用自定义 developmentOnly 配置(如上所示)是防止 devtools 被传递应用到使用你的项目的其他模块的最佳实践。

  1. 如果您使用 IntelliJ IDEA,请确保安装了 Lombok 插件 并启用了 注释处理。为了使新手更容易进行初始项目设置,您还可以根据 需要 指定 Lombok 插件。

原文由 naXa stands with Ukraine 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题