Last active
November 14, 2018 08:38
-
-
Save lihongjie0209/1eeb494453831edd1d7685df227c57ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 身份证校验规则 | |
*/ | |
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE}) | |
@Retention(RUNTIME) | |
@Constraint(validatedBy = IDCard.Validator.class) | |
@Documented | |
//注解名称 | |
public @interface IDCard { | |
// 错误消息 | |
String message() default "{cn.boolin.rental.service.validation.customValidators.IDCard.message}"; | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
// 校验实现 | |
public class Validator implements ConstraintValidator<IDCard, String> { | |
public void initialize(IDCard constraint) { | |
} | |
public boolean isValid(String obj, ConstraintValidatorContext context) { | |
IdcardValidator validator = new IdcardValidator(); | |
// null 校验请使用注解 @notnull, 身份证校验只校验身份证有效性 | |
if (Strings.isNullOrEmpty(obj)){ | |
return true; | |
} | |
return validator.isValidatedAllIdcard(obj); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment