【发布时间】:2016-06-08 13:22:15
【问题描述】:
我开始使用 bean 验证,我有以下代码来进行 bean 验证:
class Personne {
void validate(Class<?>... groupes) {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Personne>> violation = validator.validate(this, groupes);
if (violation.size() > 0) {
Set<String> violationMessages = new HashSet<String>();
for (ConstraintViolation<Personne> violations : violation) {
violationMessages.add("Valeur '" +
violations.getInvalidValue()+
"' invalid for '"+
violations.getRootBeanClass().getSimpleName()+
"."+violations.getPropertyPath()
+ "' : "+
violations.getMessage());
}
throw new RuntimeException(" class in not valid: \n" + StringUtils.join(violationMessages, "\n"));
}
}
}
与
class Eleve extends Personne
{
@NotNull
String nom
@NotNull
String prenom
}
class Prof extends Personne
{
@NotNull
String nom
@NotNull
String prenom
}
我想在异常错误的错误消息中添加正在验证的 bean 的名称类,如下所示:
throw new RuntimeException(getRootBeanClass().getSimpleName() + "is not valid: \n" + StringUtils.join(violationMessages, "\n"));
有什么想法吗?提前致谢
【问题讨论】: