我想在创建或更新之前验证多个实体。我在Grails中使用自定义验证器完成了这些操作。我想知道有没有办法在春天做自定义验证器,它将从实体中获取约束(@NotNull,@ Size等)。
示例实体: 员工:
@Entity
class Employee{
@NotNull
String firstName
@NotNull
String lastName
@NotNull
Date dob
@ManyToOne
@JoinColumn(name = "contact_id", nullable = true)
Contact contact
}
与
@Entity
class Contact{
@NotNull
String mobile
@NotNull
String email
}
控制器方法:
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
ResponseEntity create(@RequestBody Map<String, Object> payload) {
Employee employee = new Employee()
Contact contact = new Contact()
//assign some properties before validation
//validate employee and contact
// create employee and contact
}
JSON请求:
{
"employee": {
"firstName": "abc",
"lastName": "def",
"contact": {
"mobile": "9876543210",
"email": "email@example.com"
}
}
}
我想进行自定义验证,如果存在联系对象,则验证其属性。如果DOB大于特定日期,那么期望一些属性等。