• spring自定义校验注解(身份证,手机号)


    实现自定义校验注解,ConstraintValidator接口

    /**
     * Defines the logic to validate a given constraint {@code A}
     * for a given object type {@code T}.
     * <p>
     * Implementations must comply to the following restriction:
     * <ul>
     *     <li>{@code T} must resolve to a non parameterized type</li>
     *     <li>or generic parameters of {@code T} must be unbounded
     *     wildcard types</li>
     * </ul>
     * <p>
     * The annotation {@link SupportedValidationTarget} can be put on a
     * {@code ConstraintValidator} implementation to mark it as supporting
     * cross-parameter constraints. Check out {@link SupportedValidationTarget}
     * and {@link Constraint} for more information.
     *
     * @param <A> the annotation type handled by an implementation
     * @param <T> the target type supported by an implementation
     *
     * @author Emmanuel Bernard
     * @author Hardy Ferentschik
     */
    public interface ConstraintValidator<A extends Annotation, T> {
    
    	/**
    	 * Initializes the validator in preparation for
    	 * {@link #isValid(Object, ConstraintValidatorContext)} calls.
    	 * The constraint annotation for a given constraint declaration
    	 * is passed.
    	 * <p>
    	 * This method is guaranteed to be called before any use of this instance for
    	 * validation.
    	 * <p>
    	 * The default implementation is a no-op.
    	 *
    	 * @param constraintAnnotation annotation instance for a given constraint declaration
    	 */
    	default void initialize(A constraintAnnotation) {
    	}
    
    	/**
    	 * Implements the validation logic.
    	 * The state of {@code value} must not be altered.
    	 * <p>
    	 * This method can be accessed concurrently, thread-safety must be ensured
    	 * by the implementation.
    	 *
    	 * @param value object to validate
    	 * @param context context in which the constraint is evaluated
    	 *
    	 * @return {@code false} if {@code value} does not pass the constraint
    	 */
    	boolean isValid(T value, ConstraintValidatorContext context);
    }
    

      

    1.initialize方法

    会在校验实例化后被调用,一般用于做些初始化工作。

    2.isValid方法

    实际执行验证的方法,第一个参数获取的是字段或对象实际对应的值,取决于类的枚举限定类型。第二个参数是ConstraintValidatorContext,上下文可以做些默认的设置。



    本文使用自定义校验注解验证身份证是否符合要求。
     
     
    @Constraint(validatedBy = IdCardValidator.class)
    @Target({ElementType.METHOD, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface IdCard {
    
        String message() default "身份证不合法";
        Class<?>[] groups() default {};
        Class<? extends Payload>[] payload() default {};
    }
    

     

    public class IdCardValidator implements ConstraintValidator<IdCard, String> {
    
    
    
        @Override
        public void initialize(IdCard constraintAnnotation) {
        
    
        }
    
    
    
        @Override
        public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
            return IdCardUtils.checkIdCard(s);
        }
    }
    

      

        @IdCard
        private String idCard;
    

      

     

  • 相关阅读:
    对于大流量的网站,您采用什么样的方法来解决访问量问题?
    div section article区分--20150227
    不懂的code整理学习
    常用又容易忘记的代码
    【转】机器学习中常用损失函数
    姿态估计的两个数据集COCO和MPII的认识
    用caffe训练openpose过程中,出现异常
    编译caffe的诸多注意事项
    Win7下Anaconda3+Tensorflow
    论文阅读(Zhe Cao——【CVPR2017】Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields )
  • 原文地址:https://www.cnblogs.com/purely/p/15987422.html
Copyright © 2020-2023  润新知