我正在尝试使用 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")
}
我能够包含注释,并且在编辑器中包含了 lombok。我什至能够使用 lombok 编译代码并对 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;
}
这是一个编译良好的函数:
@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,则效果很好。我不知道发生了什么以及如何解决它。任何想法?
原文由 Lucas 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用最新的 Lombok 1.18 很简单。
io.franzbecker.gradle-lombok
不需要插件。我的项目中的示例依赖项:其他建议:
testCompile("junit:junit")
不是必需的,因为spring-boot-starter-test
“Starter” 包含 JUnit 。implementation
或api
配置而不是compile
(自 Gradle 3.4 起)。 我如何选择合适的?compile
配置devtools
。 Developer Tools 指南中的提示: