主要用于校验前台传输过来的数据
首先引入maven依赖
<!-- validator -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
创建测试的验证对象
/**
* @author evan_qb
* @date 2018/8/8 13:18
*/
@Data
public class TestVo {
@NotBlank(message = "msg不能为空值")
private String msg;
@NotNull(message = "编号不能为空")
@Min(value = 0,message = "最小值不能小于0")
@Max(value = 10,message = "最大值不能超过10")
private Integer id;
}
自定义一个验证的异常类ParamException重写RuntimeException默认方法
/**
* @author evan_qb
* @date 2018/8/8 13:33
*/
public class ParamException extends RuntimeException{
public ParamException() {
super();
}
public ParamException(String message) {
super(message);
}
public ParamException(String message, Throwable cause) {
super(message, cause);
}
public ParamException(Throwable cause) {
super(cause);
}
protected ParamException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
并在自定义异常处理类中添加该异常
接着编写验证的工具类BeanValidator,通过传入对象,并将验证产生的错误以Map的形式抛出异常
/**
* @author evan_qb
* @date 2018/8/8 11:24
*/
public class BeanValidator {
private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
/**
* 验证并以Map方式返回错误信息
* @param t
* @param groups
* @param <T>
* @return
*/
public static <T> Map<String,String> validate(T t,Class... groups){
//获取验证器
Validator validator = validatorFactory.getValidator();
Set validateResult = validator.validate(t,groups);
if (validateResult.isEmpty()){
return Collections.emptyMap();
}else{
LinkedHashMap errors = Maps.newLinkedHashMap();
Iterator it = validateResult.iterator();
while (it.hasNext()){
ConstraintViolation violation = (ConstraintViolation) it.next();
errors.put(violation.getPropertyPath().toString(),violation.getMessage());
}
return errors;
}
}
/**
* 验证集合
* @param collection
* @return
*/
public static Map<String,String> validateList(Collection<?> collection){
Preconditions.checkNotNull(collection);
Iterator<?> it = collection.iterator();
Map errors = null;
do{
if (!it.hasNext()){
return Collections.emptyMap();
}
Object object = it.next();
errors = validate(object,new Class[0]);
}while (errors.isEmpty());
return errors;
}
public static Map<String,String> validateObject(Object first,Object... objects){
if (objects != null && objects.length > 0){
return validateList(Lists.asList(first,objects));
}else{
return validate(first,new Class[0]);
}
}
/**
* 验证信息
* @param param
* @throws ParamException
*/
public static void check(Object param) throws ParamException{
Map<String,String> map = BeanValidator.validateObject(param);
if (MapUtils.isNotEmpty(map)){
throw new ParamException(map.toString());
}
}
}
接着测试一波:
@GetMapping("/validate.json")
@ResponseBody
public JsonData validate(TestVo vo) throws ParamException{
log.info("validate");
BeanValidator.check(vo);
return JsonData.success("test validate");
}