@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = NotBlankThenPattern.NotBlankThenPatternValidation.class)
@Documented
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;
@Override
public void initialize(NotBlankThenPattern constraintAnnotation) {
this.pattern = Pattern.compile(constraintAnnotation.regexp());
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (StringUtils.isBlank(value)) {
return true;
}
return pattern.matcher(value).matches();
}
}
}
NotBlankThenPattern非空才依据正则校验工具类
最新推荐文章于 2022-10-09 11:26:14 发布