自定义@ interface及组织校验

  


  1. ({ElementType.FIELD, ElementType.METHOD})  

  2. (RetentionPolicy.RUNTIME)  

  3. (validatedBy=MinValidator.)  

  4.   Min {  

  5.  value()  ;  

  6.  {};  

  7.  Payload>[] payload()  {};  



  1.   MinValidator  ConstraintValidator {  

  2.   minValue;  

  3.   initialize(Min min) {  

  4.   

  5.   

  6.   isValid(Integer value, ConstraintValidatorContext arg1) {  

  7.   

  8.   

  9.  value >= minValue;  

  10.   User {  

  11.   age;  


  12. (value=https://www.yisu.com/zixun/, message=)

  13.   getAge() {  

  14.  age;  

  15.   setAge( age) {  

  16. .age = age;  



二、group校验

public class Student implements Serializable {
  private  static  final  long  serialVersionUID =, 1 l;
  
  @NotBlank (message =, "名称不能为空”,,groups =, {, First.class })
  private  String 名称;
  
  @NotBlank (message =,”年龄不能为空”,,groups =, {, Second.class })
  private  String 年龄;
  
  ,,,,,,,…省略get 集方法
  }
  
  public  @interface  First  {
  }
  
  public  @interface  Second  {
  }
  
  public  static  void  main (String [], args) {,
  ,,,,Student  Student =, new 学生(),,
  ,,,,ValidatorFactory  vf =, Validation.buildDefaultValidatorFactory ();
  ,,,,Validator  Validator =, vf.getValidator (),,
  ,,,,Set祝辞,set =, validator.validate(学生,First.class);,
  ,,,,for  (ConstraintViolation 



自定义@ interface及组织校验