使用龙目岛与格雷尔和弹簧靴

2022-09-01 13:54:53

我正在尝试用龙目岛构建一个项目,这就是我作为依赖项所拥有的。

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")
}

我能够包括注释,并且我已经在编辑器中包括了龙目岛。我甚至能够使用龙目岛编译代码,并对龙目岛生成的方法进行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;
}

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

@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 和 getters,这工作正常。我不明白发生了什么以及如何解决它。有什么想法吗?


答案 1

使用最新的龙目岛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")不是必需的,因为“启动器”包含 JUnitspring-boot-starter-test
  2. 使用或配置代替(从Gradle 3.4开始)。如何选择合适的?implementationapicompile
  3. 不要对 使用 配置。开发人员工具指南中的提示:compiledevtools

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

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


答案 2

在为此苦苦挣扎了一段时间后,我发现这个插件可以做到这一点:

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"
}

我以前遇到过这个插件,但我做错了什么,它不起作用。我很高兴它现在起作用了。

谢谢!


推荐