@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy =MyConstraintValidator.class ) public @interface MyAnnotation { String message() default "王某自定义注解"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; }
@Slf4j public class MyConstraintValidator implements ConstraintValidator<MyAnnotation,Object> { private final String MOBILE_REGEXP = "^[1][3,4,5,6,7,8,9][0-9]{9}$"; @Override public void initialize(MyAnnotation constraintAnnotation) { log.info("初始化失败!"); } @Override public boolean isValid(Object value, ConstraintValidatorContext constraintValidatorContext) { // if(flag){ // //TODO 做自己的验证 业务操作 // //TRUE标识返回成功 // } String strValue = String.valueOf(value); if (ObjectUtils.isEmpty(value)) { return Pattern.matches(MOBILE_REGEXP, strValue); } return false; } }
public class AdminUser { private String name; private String password; @MyAnnotation private String phone; }
注解介绍请看;https://www.cnblogs.com/wangbiaohistory/p/12522153.html
标签:String,自定义,spring,private,class,注解,public From: https://www.cnblogs.com/wangbiaohistory/p/16913063.html