使用lombok与gradle和spring-boot [英] Using lombok with gradle and spring-boot
问题描述
我正在尝试使用lombok构建一个项目,这就是我所拥有的依赖项目。
I am trying to build a project with lombok and this is what I have as dependencie.
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设置为lombok生成的方法。
I am able to include the anotations, and I have included lombok in the editor. I am even able to compile a code using lombok and making a cal to a method generated by 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;
}
这是一个编译好的函数:
And this is a function that compiles fine:
@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并尝试访问功能时,这就是异常我得到:
But when I run bootRun and I try to access the funcionality this is the Exception I get:
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,这样可以正常工作。我不知道怎么回事以及如何解决它。有什么想法吗?
But, if I manually include the setter and getters, this works fine. I don't get whats going on and how to fix it. Any idea?
推荐答案
经过一段时间的努力,我发现这个插件可以解决问题:
After struggling with this for a while I found this plugin would do the trick:
https://github.com/franzbecker/gradle-lombok
我的gradle文件看起来像这样:
My gradle file just looks like this:
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"
}
<我以前遇到过这个插件,但是我做错了什么都没用。我很高兴它现在有效。
I had come across this plugin before but I did something wrong and it didn't work. I am glad it worked now.
谢谢!
这篇关于使用lombok与gradle和spring-boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!