码迷,mamicode.com
首页 > 编程语言 > 详细

springboot-自定义校验

时间:2019-11-25 15:31:03      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:自定义   init   with   add   OLE   private   xtend   ddr   parameter   

申明:摘录自java知音

 

StartWithValidation.class

@Documented
@Constraint(validatedBy = StartWithValidator.class )
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface StartWithValidation {
String message() default "不是正确的性别取值范围";
String start() default "_";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@interface List {
StartWithValidation[] value();
}
}

StartWithValidator.class

public class StartWithValidator implements ConstraintValidator<StartWithValidation, String> {
private String start;
@Override
public void initialize(StartWithValidation constraintAnnotation) {
start = constraintAnnotation.start();
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

if (!StringUtils.isEmpty(value)) {
return value.startsWith(start);
}
return true;
}
}
运用
@StartWithValidation(message = "Param ‘address‘ must be start with ‘Beijing‘.", start = "Beijing")
public String address;

解释:
地址必须以Beijing开头,否则校验失败

springboot-自定义校验

标签:自定义   init   with   add   OLE   private   xtend   ddr   parameter   

原文地址:https://www.cnblogs.com/SoftwareBuilding/p/11927688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!