java – 使用带有gradle和spring-boot的lombok

前端之家收集整理的这篇文章主要介绍了java – 使用带有gradle和spring-boot的lombok前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用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")
}

我能够包含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;
}

这是一个编译好的函数

@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,这样可以正常工作.我不知道怎么回事以及如何解决它.任何的想法?

解决方法

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

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

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

谢谢!

原文链接:/java/121300.html

猜你在找的Java相关文章