使用lombok与gradle和spring-boot

Luc*_*cas 16 java gradle lombok 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")
}
Run Code Online (Sandbox Code Playgroud)

我能够包含anotations,并且我在编辑器中包含了lombok.我甚至能够使用lombok编译代码并对由lombok生成的方法进行cal.

这是我的实体:

@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;
}
Run Code Online (Sandbox Code Playgroud)

这是一个编译好的函数:

@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";
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行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?
Run Code Online (Sandbox Code Playgroud)

但是,如果我手动包含setter和getter,这样可以正常工作.我不知道怎么回事以及如何解决它.任何的想法?

naX*_*aXa 21

使用最新的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'
}
Run Code Online (Sandbox Code Playgroud)

其他建议:

  1. testCompile("junit:junit")不是必需的,因为spring-boot-starter-test"Starter" 包含JUnit.
  2. 使用implementationapi配置代替compile(自Gradle 3.4起).我该如何选择合适的?
  3. 不要使用compile配置devtools.开发者工具指南中的提示:

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

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

  • 谢谢你。annotationProcessor 'org.projectlombok:lombok' 为我解决了这个问题 (3认同)

Luc*_*cas 14

经过一段时间的努力,我发现这个插件可以解决这个问题:

https://github.com/franzbecker/gradle-lombok

我的gradle文件看起来像这样:

buildscript {
    repositories {
        maven { url "https://repo.spring.io/libs-milestone" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
        classpath 'org.springframework:springloaded:1.2.6.RELEASE'
    }
}

plugins {
    id 'io.franzbecker.gradle-lombok' version '1.8'
    id 'java'
}



apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'



jar {
    baseName = 'gs-accessing-facebook'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/libs-milestone" }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // compile("org.projectlombok:lombok:1.16.10")
    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")
}

idea {
    module {
        inheritOutputDirs = false
        outputDir = file("$buildDir/classes/main/")
    }
}

bootRun {
    addResources = true
    jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
}
Run Code Online (Sandbox Code Playgroud)

之前我曾经遇到过这个插件,但是我做错了什么并且没用.我很高兴它现在奏效了.

谢谢!


jum*_*key 6

如果您正在使用Eclipse或其一个分支(我正在使用 Spring Tool Suite 4.5.1.RELEASE, - 同样的步骤适用于另一个其他版本),您需要做的就是:

  • build.gradle

    dependencies {
      compileOnly 'org.projectlombok:lombok'  
      annotationProcessor 'org.projectlombok:lombok'
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 然后,右键单击您的项目 > Gradle > 刷新 Gradle 项目。该lombok-"version".jar出现项目的内部项目和外部依赖性

  • 右键单击该lombok-"version".jar> 运行方式 > Java 应用程序(类似于双击实际 jar 或java -jar lombok-"version".jar在命令行上运行。)

  • 将出现一个 GUI,按照所有说明进行操作,它所做的一件事就是将其复制lombok.jar到 IDE 的根文件夹中。

更新:

1 年后,使用4.11.1.RELEASE,我仍然指的是我写的安装 Lombok 的内容。