[html] view plain copy
4.0.0
com.fei
validation-test
0.0.1-SNAPSHOT
javax.el
javax.el-api
2.2.4
org.hibernate
hibernate-validator
5.1.3.Final
2.基本校验练习
StudentInfo.java
[java] view plain copy
package com.fei.info;
import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.NotBlank;public classStudentInfo {
@NotBlank(message="用户名不能为空")privateString userName;
@NotBlank(message="年龄不能为空")
@Pattern(regexp="^[0-9]{1,2}$",message="年龄是整数")privateString age;/**
* 如果是空,则不校验,如果不为空,则校验*/@Pattern(regexp="^[0-9]{4}-[0-9]{2}-[0-9]{2}$",message="出生日期格式不正确")privateString birthday;
@NotBlank(message="学校不能为空")privateString school;publicString getUserName() {returnuserName;
}public voidsetUserName(String userName) {this.userName =userName;
}publicString getAge() {returnage;
}public voidsetAge(String age) {this.age =age;
}publicString getBirthday() {returnbirthday;
}public voidsetBirthday(String birthday) {this.birthday =birthday;
}publicString getSchool() {returnschool;
}public voidsetSchool(String school) {this.school =school;
}
}
ValidatorUtil.java
[java] view plain copy
package com.fei.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;public classValidatorUtil {private static Validator validator =Validation.buildDefaultValidatorFactory()
.getValidator();public static Mapvalidate(T obj){
Map errorMap = null;
Set> set = validator.validate(obj,Default.class);if(set != null && set.size() >0){
errorMap= new HashMap();
String property= null;for(ConstraintViolation cv : set){//这里循环获取错误信息,可以自定义格式
property =cv.getPropertyPath().toString();if(errorMap.get(property) != null){
errorMap.get(property).append("," +cv.getMessage());
}else{
StringBuffer sb= newStringBuffer();
sb.append(cv.getMessage());
errorMap.put(property, sb);
}
}
}returnerrorMap;
}
}
ValidatorTest.java
[java] view plain copy
package com.fei;
import java.util.Map;
import com.fei.info.StudentInfo;
import com.fei.util.ValidatorUtil;public classValidatorTest {public static voidmain(String[] args) {
StudentInfo s= newStudentInfo();long startTime =System.currentTimeMillis();
print(ValidatorUtil.validate(s));
System.out.println("===============耗时(毫秒)=" + (System.currentTimeMillis() -startTime));
s.setUserName("小明");
s.setAge("a10");
s.setBirthday("2016-9-1");
startTime=System.currentTimeMillis();
print(ValidatorUtil.validate(s));
System.out.println("===============耗时(毫秒)=" + (System.currentTimeMillis() -startTime));
}private static void print(MaperrorMap){if(errorMap != null){for(Map.Entrym : errorMap.entrySet()){
System.out.println(m.getKey() + ":" +m.getValue().toString());
}
}
}
}
来看看运行结果:
[plain] view plain copy
十二月12, 2016 4:02:00 下午 org.hibernate.validator.internal.util.Version INFO: HV000001: Hibernate Validator5.1.3.Final
school:学校不能为空
age:年龄不能为空
userName:用户名不能为空===============耗时(毫秒)=280birthday:出生日期格式不正确
school:学校不能为空
age:年龄是整数===============耗时(毫秒)=3