【发布时间】:2019-02-12 12:23:34
【问题描述】:
这是我要验证的 TO 类:
public class Person {
@NotNull
private String name;
@NotNull
@Pattern(regexp = ID.REGEX)
private String id;
public Person(String name, String id) {
this.name = name;
this.id = id;
}
}
及验证方法:
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Person person = new Person(null, "notCorrextIdRegex");
validator.validate(person).stream().forEach(constraintViolation -> {
...
}
是否可以轻松获取验证失败的注解类型?我需要这个来创建具有各种错误代码的异常,具体取决于constraintViolation,例如:
@NotNull -> new Exception("Object is null or empty.")
这是实现它的好方法吗?
【问题讨论】:
标签: hibernate validation