Java 在Hibernate验证程序中为每个属性生成错误代码
我正在考虑使用Hibernate验证器满足我的一个需求。我想验证一个JavaBean,其中属性可能有多个验证检查。例如:Java 在Hibernate验证程序中为每个属性生成错误代码,java,validation,hibernate-validator,Java,Validation,Hibernate Validator,我正在考虑使用Hibernate验证器满足我的一个需求。我想验证一个JavaBean,其中属性可能有多个验证检查。例如: class MyValidationBean { @NotNull @Length( min = 5, max = 10 ) private String myProperty; } 但是,如果此属性验证失败,我希望特定的错误代码与ConstraintViolation关联,不管它是因为@Required还是@Length而失败,尽管我希望保留错误消息 c
class MyValidationBean
{
@NotNull
@Length( min = 5, max = 10 )
private String myProperty;
}
但是,如果此属性验证失败,我希望特定的错误代码与ConstraintViolation关联,不管它是因为@Required还是@Length而失败,尽管我希望保留错误消息
class MyValidationBean
{
@NotNull
@Length( min = 5, max = 10 )
@ErrorCode( "1234" )
private String myProperty;
}
像上面这样的东西会很好,但它的结构不必完全像那样。我看不到用Hibernate验证器实现这一点的方法。可能吗?从本规范的章节:
getMessageTemplate
方法返回非插值错误消息(通常是约束声明上的message
属性)。框架可以将其用作错误代码键
我认为这是您最好的选择。我将尝试在应用程序的DAO层隔离此行为 以您为例,我们将:
public class MyValidationBeanDAO {
public void persist(MyValidationBean element) throws DAOException{
Set<ConstraintViolation> constraintViolations = validator.validate(element);
if(!constraintViolations.isEmpty()){
throw new DAOException("1234", contraintViolations);
}
// it's ok, just persist it
session.saveOrUpdate(element);
}
}
公共类MyValidationBeanDAO{
公共void persist(MyValidationBean元素)引发异常{
Set constraintViolations=validator.validate(元素);
如果(!constraintViolations.isEmpty()){
抛出新的异常(“1234”,违例);
}
//没关系,坚持下去
session.saveOrUpdate(元素);
}
}
以及以下异常类:
public class DAOException extends Exception {
private final String errorCode;
private final Set<ConstraintViolation> constraintViolations;
public DAOException(String errorCode, Set<ConstraintViolation> constraintViolations){
super(String.format("Errorcode %s", errorCode));
this.errorCode = errorCode;
this.constraintViolations = constraintViolations;
}
// getters for properties here
}
public类异常扩展异常{
私有最终字符串错误代码;
私有最终集约束冲突;
公共DAO异常(字符串错误代码,设置约束条件){
超级(字符串格式(“错误代码%s”,错误代码));
this.errorCode=错误代码;
this.constraintViolations=constraintViolations;
}
//这里是属性的getter
}
您可以根据此处未验证的属性添加一些注释信息,但始终在DAO方法上这样做
我希望这会有所帮助。您可以创建一个自定义注释,以获取您正在寻找的行为,然后在验证和使用refelection时,您可以提取注释的值。如下所示:
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ErrorCode {
String value();
}
在您的bean中:
@NotNull
@Length( min = 5, max = 10 )
@ErrorCode("1234")
public String myProperty;
Set<ConstraintViolation<MyValidationBean>> constraintViolations = validator.validate(myValidationBean);
for (ConstraintViolation<MyValidationBean>cv: constraintViolations) {
ErrorCode errorCode = cv.getRootBeanClass().getField(cv.getPropertyPath().toString()).getAnnotation(ErrorCode.class);
System.out.println("ErrorCode:" + errorCode.value());
}
验证bean时:
@NotNull
@Length( min = 5, max = 10 )
@ErrorCode("1234")
public String myProperty;
Set<ConstraintViolation<MyValidationBean>> constraintViolations = validator.validate(myValidationBean);
for (ConstraintViolation<MyValidationBean>cv: constraintViolations) {
ErrorCode errorCode = cv.getRootBeanClass().getField(cv.getPropertyPath().toString()).getAnnotation(ErrorCode.class);
System.out.println("ErrorCode:" + errorCode.value());
}
Set constraintViolations=validator.validate(myValidationBean);
对于(ConstraintViolationcv:constraintViolations){
ErrorCode ErrorCode=cv.getRootBeanClass().getField(cv.getPropertyPath().toString()).getAnnotation(ErrorCode.class);
System.out.println(“ErrorCode:+ErrorCode.value());
}
话虽如此,我可能会对这些类型的消息需要错误代码的要求提出疑问。谢谢您的回复。不幸的是,我认为这不会保留我想要的原始错误消息。我正在寻找一个额外的错误代码在此之上。遗憾的是,看看ConstraintViolation的API,我并没有看到任何有希望的东西。这是一个很好的解决方案,非常感谢您的发布。只有一件事需要注意。。代码应读取getDeclaredField以使其能够访问私有字段。