如何在自定义验证注释上插入 ConstraintValidator 消息
How to interpolate the ConstraintValidator message on a Custom Validation Annotation
我已经阅读了一些关于如何使用 Hibernate 的 ConstrainValidator 的问答和指南(例如 Hibernate Documentation),但是其中 none 清楚地提到了如何在 [=22] 中插入一个值=]创建您自己的自定义验证注释时验证错误消息的特定位置。
例如,如果我收到如下所示的验证错误消息:
foo.bar.error=This value '{myValue}' is wrong.
如果验证失败,我想获得以下信息:
The value 'some wrong value' is wrong.
验证将像这样使用:
public class SomeClass {
@CustomAnnotation(message="{foo.bar.error}")
public MyObject myObject;
...
}
public class MyObject {
private String myValue;
...
}
我找到了一种插入消息的方法,没有太多模糊。
首先像以前一样设置您的 ValidationMessages.properties
(或存储验证消息的任何位置):
foo.bar.error=This value '{wrongValue}' is wrong.
像往常一样创建注释:
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Constraint(validatedBy = CustomValueValidator.class)
@Target(FIELD)
@Retention(RUNTIME)
public @interface CustomAnnotation {
String message() default "{foo.bar.error}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
像这样实现你的约束验证器:
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CustomValueValidator
implements ConstraintValidator<CustomAnnotation, MyObject> {
@Override
public void initialize(final CustomAnnotation constraintAnnotation) {
// Extract any value you would need from the annotation
}
@Override
public boolean isValid(final MyObject myObject,
final ConstraintValidatorContext context) {
boolean valid;
// Add your validations over the 'myObject' object
if (!valid) {
((ConstraintValidatorContextImpl) context)
.addMessageParameter("wrongValue", myObject.getMyValue());
}
return valid;
}
}
现在剩下的就是使用注解了:
public class SomeClass {
// I'm using the default message. You could override it as a parameter
@CustomAnnotation
public MyObject anotherObject;
...
}
我已经阅读了一些关于如何使用 Hibernate 的 ConstrainValidator 的问答和指南(例如 Hibernate Documentation),但是其中 none 清楚地提到了如何在 [=22] 中插入一个值=]创建您自己的自定义验证注释时验证错误消息的特定位置。
例如,如果我收到如下所示的验证错误消息:
foo.bar.error=This value '{myValue}' is wrong.
如果验证失败,我想获得以下信息:
The value 'some wrong value' is wrong.
验证将像这样使用:
public class SomeClass {
@CustomAnnotation(message="{foo.bar.error}")
public MyObject myObject;
...
}
public class MyObject {
private String myValue;
...
}
我找到了一种插入消息的方法,没有太多模糊。
首先像以前一样设置您的 ValidationMessages.properties
(或存储验证消息的任何位置):
foo.bar.error=This value '{wrongValue}' is wrong.
像往常一样创建注释:
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Constraint(validatedBy = CustomValueValidator.class)
@Target(FIELD)
@Retention(RUNTIME)
public @interface CustomAnnotation {
String message() default "{foo.bar.error}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
像这样实现你的约束验证器:
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
public class CustomValueValidator
implements ConstraintValidator<CustomAnnotation, MyObject> {
@Override
public void initialize(final CustomAnnotation constraintAnnotation) {
// Extract any value you would need from the annotation
}
@Override
public boolean isValid(final MyObject myObject,
final ConstraintValidatorContext context) {
boolean valid;
// Add your validations over the 'myObject' object
if (!valid) {
((ConstraintValidatorContextImpl) context)
.addMessageParameter("wrongValue", myObject.getMyValue());
}
return valid;
}
}
现在剩下的就是使用注解了:
public class SomeClass {
// I'm using the default message. You could override it as a parameter
@CustomAnnotation
public MyObject anotherObject;
...
}