自定义validator 注解

2019-08-20  本文已影响0人  茧铭

记录一个自定义校验注解,作用是验证该字段是不是一个正确的手机号码的格式

/**
 * @program: jpademo
 * @description: TelphoneValidator
 * @author: ZengGuangfu
 * @create 2018-10-25 14:42
 */

@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyTelphoneValidator.class)
public @interface Telphone {

    /** 一个是否验证的开关,默认要验证 */
    boolean isValidetor() default true;

    /** 默认的报错信息 */
    String message() default "这不是一个正确的手机号码";

    /** 分组信息,可通过空内容的接口去实现分组验证 */
    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}
/**
 * @program: jpademo
 * @description: MyTelphoneValidator
 * @author: ZengGuangfu
 * @create 2018-10-25 14:45
 */
public class MyTelphoneValidator implements ConstraintValidator<Telphone,String> {
    private String telphoneNumberValid = "/^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$/";

    //可以调用上层的这个方法设置一个默认值,也可以自己传值
    //如果传值了,不是用的默认值的话,false就代表不进行验证,true就代表要验证,这个可以在isValid()方法中进行验证
    boolean isValidetor;

    /**
    * 加载方法,准备校验
    */
    @Override
    public void initialize(Telphone telphone) {
        System.out.println("自定义validator加载完成");
        isValidetor = telphone.isValidetor();
    }
    /**
    * 返回true表示验证通过,false则不通过,提示错误信息message()
    */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(isValidetor){
            if (value == null) {
                return false;
            }
            return Pattern.matches(telphoneNumberValid,value);
        }else{
            return true;
        }
    }
}

分组验证举例

字段有分类Gourp1.class (Gourp1是一个空内容的接口)

/**
 * @program: jpademo
 * @description: Grade Entity
 * @author: ZengGuangfu
 * @create 2018-10-24 09:09
 */

@Entity
@Table(name = "t_grade")
@Data
public class Grade {
    /**
     * 主键
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "grade_id")
    private Integer gradeId;

    /**
     * 班级
     */
    @Column(nullable = false,name = "remo")
    @Range(min = 1,max = 30,groups = {Group2.class},message = "班级号remo超过1-30的限制")
    private Integer remo;

    /**
     * 班主任
     */
    @Column(unique = true)
    @NotNull(message = "班主任不能为空")
    private String teacher;

    /**
     * 楼层
     */
    @Column(name = "floor")
    @Min(value = 5,message = "楼层最小为5",groups = {Group1.class})
    private Integer floor;

}

@RestController 仅验证分组Group1.class

@PostMapping
    public String insert(@Validated(Group1.class) Grade grade, BindingResult results){
        if(results!=null && results.hasErrors()){
            List<ObjectError> globalErrors = results.getAllErrors();
            for (ObjectError error :globalErrors
                 ) {
                System.out.println("-----------"+error.getDefaultMessage());
                return SysNode.Judge.FAILD.getResult();
            }
        }
        Integer inse = gradeService.insert(grade);
        return inse == 0 ? SysNode.Judge.FAILD.getResult() : SysNode.Judge.SUCCESS.getResult();
    }
上一篇 下一篇

猜你喜欢

热点阅读