概要
最近在使用hibernate-validator针对VO做一些简单校验. 其中有一个业务场景是需要校验日期参数必须大于某一天, 比如1970-01-01 08:30:00等.
初步的方案是先创建一个注解类, 比如@DateAfter, 再指定@DateAfter的校验器DateAfterValidator, 再然后实现该校验器DateAfterValidator.
DateAfter.java
<pre name="code" class="java">package com.feisheng.annotation;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;import static java.lang.annotation.ElementType.CONSTRUCTOR;import static java.lang.annotation.ElementType.FIELD;import static java.lang.annotation.ElementType.METHOD;import static
java.lang.annotation.ElementType.PARAMETER;import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.Documented;import java.lang.annotation.Retention;import java.lang.annotation.Target;import javax.validation.Constraint;import
javax.validation.Payload;import com.vip.vpal.zc.admin.validator.DateAfterValidator;/** * The annotated element must be a date, and after the specified date. * Only accepts Date. * @author eric.mo */@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER
})@Retention(RUNTIME)@Documented@Constraint(validatedBy = { DateAfterValidator.class })public @interface DateAfter { String message() default "时间取值不正确"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; long value() default 31536000000L;
/** * Defines several {@link DateAfter} annotations on the same element. * * @see DateAfter */ @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) @Retention(RUNTIME) @Documented @interface List { DateAfter[] value(); }}
DateAfterValidator.java
package com.feisheng.validator;
import java.util.Date;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import com.vip.vpal.zc.admin.annotation.DateAfter;
/**
* The annotated element must be a date, and after the specified date.
* Only accepts Date.
* @author eric.mo
*/
public class DateAfterValidator implements ConstraintValidator<DateAfter, Date> {
private Long setDateTime;
@Override
public void initialize(DateAfter constraintAnnotation) {
setDateTime = constraintAnnotation.value();
}
@Override
public boolean isValid(Date value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
return value.after(new Date(setDateTime));
}
}
使用方式
@DateAfter
private Date sellStartTime; //直接添加注解
然后再使用hibernate-validator校验即可.
package com.vip.vpal.zc.admin.validator;import java.util.Date;import javax.validation.ConstraintValidator;import javax.validation.ConstraintValidatorContext;import
com.vip.vpal.zc.admin.annotation.DateAfter;/** * The annotated element must be a date, and after the specified date. * Only accepts Date. * @author eric.mo */public class DateAfterValidator implements ConstraintValidator<DateAfter, Date> { private Long setDateTime;
@Override public void initialize(DateAfter constraintAnnotation) { setDateTime = constraintAnnotation.value(); } @Override public boolean isValid(Date value, ConstraintValidatorContext context) { if (value == null) { return true; } return value.after(new Date(setDateTime));
}}