【发布时间】:2016-09-26 18:53:45
【问题描述】:
我正在尝试使用注释在我的 REST bean 上添加一些额外的验证逻辑。这只是一个示例,但重点是要在多个 REST 资源对象/DTO 上使用注释。
我希望有这样的解决方案:
public class Entity {
@NotNull // JSR-303
private String name;
@Phone // Custom phonenumber that has to exist in a database
private String phoneNumber;
}
@Component
public class PhoneNumberValidator implements Validator { // Spring Validator
@Autowired
private PhoneRepository repository;
public boolean supports(Class<?> clazz) {
return true;
}
public void validate(Object target, Errors errors) {
Phone annotation = // find fields with annotations by iterating over target.getClass().getFields().getAnnotation
Object fieldValue = // how do i do this? I can easily get the annotation, but now I wish to do a call to repository checking if the field value exists.
}
}
【问题讨论】:
标签: java spring validation spring-mvc jsr