我编写了一个名为 @Year
的自定义约束,它检查 Date
是否出现在某一年。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Constraint(validatedBy = YearCheck.class)
public @interface Year {
String message() default "{year.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
/**
* The lower year. If not specified then no upper boundary check is performed.<br>
* Special values are:
* <ul>
* <li>{@link TimeUtil#NOW} for the current year
* </ul>
*/
String min() default "";
/**
* The upper year. If not specified then no upper boundary check is performed.<br>
* Special values are:
* <ul>
* <li>{@link TimeUtil#NOW} for the current year
* </ul>
*/
String max() default "";
}
ValidationMessages.properties
包含以下内容:
year.message = must be a year between {min} and {max}
如您所见,min
和 max
是字符串,因为 TimeUtil.NOW
(包含 "now"
) 表示以当前年份进行比较。
另外,如果没有指定min
或max
,则表示对应的值为无穷大
所以,问题是,例如对于 min
:我如何检查 min
是否已设置以及是否不是数字(""
或 "now"
) 如何设置一个值,然后将其插入到消息中?
例如@Year(min=1900,max=TimeUtil.NOW)
应该生成消息
must be a year between 1900 and 2013
我已经阅读了 stackoverflow 上的一些答案,阅读了文档,但我不确定 a) 是否可能 b) 我需要在约束实现或自定义 MessageInterpolator
中执行此操作.
最佳答案
How can I check whether min has been set and if's not a number
您可以在 ConstraintValidator
实现的 initialize()
方法中访问注释属性。
how can I set a value which will then be inserted into the message?
您不能直接执行此操作,但您可以使用传递的 ConstraintValidatorContext
在 validator 的 isValid()
方法中自行创建消息。
您的 validator 可能如下所示:
public class YearValidator implements ConstraintValidator<Year, String> {
private Date min;
private Date max;
@Override
public void initialize(Year constraintAnnotation) {
if(constraintAnnotation.min().equals("")) {
min = getMinimumDate();
}
else if(constraintAnnotation.min().equals(TimeUtil.NOW)) {
min = getCurrentYear();
}
else {
min = getYearFromString(constraintAnnotation.min());
}
//same for max()
}
@Override
public boolean isValid(Date value, ConstraintValidatorContext context) {
if(value == null) {
return true;
}
if(value.before(min) || value.after(max)) {
context.disableDefaultConstraintViolation();
//load/create the error message and set min and max in it
String template = getTemplate(min, max);
context
.buildConstraintViolationWithTemplate(template)
.addConstraintViolation();
return false;
}
return true;
}
}
关于java - Bean验证: Complex message value resolution - but how?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14336310/