作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个POJO并启用了JSR验证。
出于演示的原因,我已经完全重新实现了Kotlin的整个项目,并且我想演示验证方面。只是,在Kotlin中,它根本不起作用,原因也不明显。有人可以解释我所缺少的吗?
启用验证的Java:
@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);
}
}
@Getter
@EqualsAndHashCode
@Entity
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Identifiable<Long>{
@NotNull
@Pattern(regexp = "[A-Za-z0-9]+")
@Size(min = 2, max = 32)
private String name;
@Email
@NotNull
private String email;
@NotNull
@PastOrPresent
private LocalDate hireDate = LocalDate.now();
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<Form> forms = Collections.emptyList();
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<Report> reports = Collections.emptyList();
@Id
@GeneratedValue
private Long id;
public Employee(String name, String email) {
this.name = name;
this.email = email;
}
public Employee(String name) {
this(name,name +"@sterlingts.com");
}
}
@Configuration
class ValidationConfiguration(private val jsr303Validator: Validator) : RepositoryRestConfigurerAdapter() {
override fun configureValidatingRepositoryEventListener(validatingListener: ValidatingRepositoryEventListener) {
//bean validation always before save and create
validatingListener.addValidator("beforeCreate", jsr303Validator)
validatingListener.addValidator("beforeSave", jsr303Validator)
}
}
@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")
}
POST /employees
{
"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"
}
]
}
POST /employees
{
"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"
}
}
}
最佳答案
在data class A(@Email val email: String)
中,注释默认情况下应用于构造函数参数。换句话说,它等效于:
class A{
public A(@Email String email) { ... }
}
data class A(@field:Email val email: String)
关于java - Pojo验证有效。波科( Kotlin )没有。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993437/
我有一个看起来像这样的出租车列表: 1204725 2162 1300163 420247 我希望从上面的taxids中按顺序获得一个带有分类ID的文件: kingdom_id phylum
我有物种的分类 ID,我可以从 NCBI ( https://www.ncbi.nlm.nih.gov/Taxonomy/TaxIdentifier/tax_identifier.cgi ) 获得物种
我是一名优秀的程序员,十分优秀!