2016-11-07 152 views
2

我正在嘗試與lombok建立一個項目,這是我作爲依賴項。使用lombok與gradle和彈簧引導

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生成的方法進行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,我嘗試訪問的funcionality這是例外,我得到:

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,這工作正常。我不知道發生了什麼,以及如何解決它。任何想法?

回答

4

與此掙扎了一會兒後,我發現這個插件會做的伎倆:

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

我曾經碰到過這個插件之前,但我做了錯誤的事情,但沒有奏效。我很高興它現在工作。

謝謝!

+0

我們也有這個設置,不需要額外的插件。你在使用哪個IDE?你應該爲你安裝插件IDE我猜? https://projectlombok.org/download.html – questionare

+0

這個插件幫了我很多,謝謝! 你能解釋爲什麼會發生?在Maven項目中,添加一個與lombok就足夠了,但在這裏,在gradle中,我們需要一些奇怪的插件而不是那個。 –