Java Pojo验证工作。博科(科特林)没有';T为什么?
我有一个POJO并启用了JSR验证 出于演示的原因,我已经将整个项目重新实现为Kotlin,我想演示一下验证方面。除了,在科特林,它根本不起作用,原因也不清楚。有人能解释一下我遗漏了什么吗 要启用验证,请使用Java:Java Pojo验证工作。博科(科特林)没有';T为什么?,java,kotlin,spring-data,spring-data-rest,bean-validation,Java,Kotlin,Spring Data,Spring Data Rest,Bean Validation,我有一个POJO并启用了JSR验证 出于演示的原因,我已经将整个项目重新实现为Kotlin,我想演示一下验证方面。除了,在科特林,它根本不起作用,原因也不清楚。有人能解释一下我遗漏了什么吗 要启用验证,请使用Java: @Configuration @RequiredArgsConstructor public static class ValidationConfiguration extends RepositoryRestConfigurerAdapter { private f
@Configuration
@RequiredArgsConstructor
public static class ValidationConfiguration extends RepositoryRestConfigurerAdapter {
private final Validator jsr303Validator;
@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
//bean validation always before save and create
validatingListener.addValidator("beforeCreate", jsr303Validator);
validatingListener.addValidator("beforeSave", jsr303Validator);
}
}
POJO:
博科:
结果(预期的java版本):
现在,作为Kotlin Poko:
职位/雇员
结果(201已创建):
在数据类A(@Email val Email:String)
中,默认情况下,注释应用于构造函数参数。换句话说,它相当于:
class A{
public A(@Email String email) { ... }
}
在本例中,您希望将注释应用于字段,因此需要编写:
data class A(@field:Email val email: String)
有关中注释的详细信息
@Entity
data class Employee(@Pattern(regexp = "[A-Za-z0-9]+")
@Size(min = 6, max = 32)
val name: String,
@Email
@NotNull
val email: String?,
@PastOrPresent
val hireDate: LocalDate = LocalDate.now(),
@OneToMany(mappedBy = "employee", cascade = [CascadeType.ALL])
val forms:List<Form> = listOf(),
@OneToMany(mappedBy = "employee", cascade = [CascadeType.ALL])
val reports:List<Report> = listOf(),
@Id @GeneratedValue( strategy = GenerationType.IDENTITY) private val id: Long? = null): Identifiable<Long> {
override fun getId() = id
constructor(name:String): this(name,"$name@sterlingts.com")
}
{
"name": "christian",
"email": "This is not an email"
}
{
"errors": [
{
"entity": "Employee",
"property": "email",
"invalidValue": "This is not an email",
"message": "must be a well-formed email address"
}
]
}
{
"name": "christian",
"email": "This is not an email"
}
{
"name" : "christian",
"email" : "This is not an email",
"hireDate" : "2018-02-26",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/19"
},
"employee" : {
"href" : "http://localhost:8080/employees/19"
},
"forms" : {
"href" : "http://localhost:8080/employees/19/forms"
},
"reports" : {
"href" : "http://localhost:8080/employees/19/reports"
}
}
}
class A{
public A(@Email String email) { ... }
}
data class A(@field:Email val email: String)