METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})({
(RetentionPolicy.RUNTIME)
(validatedBy = NotBlankThenPattern.NotBlankThenPatternValidation.class)
public @interface NotBlankThenPattern {
String regexp() default "";
String message() default "格式错误";
/**
* @return the groups the constraint belongs to
*/
Class<?>[] groups() default {};
/**
* @return the payload associated to the constraint
*/
Class<? extends Payload>[] payload() default {};
class NotBlankThenPatternValidation implements ConstraintValidator<NotBlankThenPattern, String> {
private Pattern pattern;
public void initialize(NotBlankThenPattern constraintAnnotation) {
this.pattern = Pattern.compile(constraintAnnotation.regexp());
}
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
return pattern.matcher(value).matches();
}
}
}