validate

Posted brxhqs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了validate相关的知识,希望对你有一定的参考价值。

package com.brx.utils;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.brx.exception.ParamException;
import org.apache.commons.collections.MapUtils;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.*;

public class BeanValidator {

  //1.创建validator工厂
private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();

public static <T> Map<String, String> validate(T t, Class... groups) {
    //2.获取Validator
Validator validator = validatorFactory.getValidator();
    //3.校验 获得的Set<ConstraintViolation> -----约束违反的set集合
Set validateResult = validator.validate(t, groups);//这个gropus感觉没什么用,主要是为了迎合参数列表,另外,t不能使集合类型
if (validateResult.isEmpty()) {
return Collections.emptyMap();
} else {
LinkedHashMap errors = Maps.newLinkedHashMap();
Iterator iterator = validateResult.iterator();
while (iterator.hasNext()) {
ConstraintViolation violation = (ConstraintViolation)iterator.next();
          4.封装结果集 (属性值-错误信息)-》(violation.getPropertyPath().toString(),violation.getMessage())
errors.put(violation.getPropertyPath().toString(), violation.getMessage());
}
return errors;
}
}

//对一个集合类型进行遍历校验,上面的validate方法,不能处理集合类型
public static Map<String, String> validateList(Collection<?> collection) {
Preconditions.checkNotNull(collection);
Iterator iterator = collection.iterator();
Map errors;

do {
if (!iterator.hasNext()) {
return Collections.emptyMap();
}
Object object = iterator.next();
errors = validate(object, new Class[0]);
} while (errors.isEmpty());//只要集合内有一个字段错误,那么直接返回,不用考虑剩余元素

return errors;
}

//通用方法,至少传入一个Object参数,这个方法还是有问题,如果first为集合就
//  BeanValidator.validateObject(testVo,testVo,testVo);
public static Map<String, String> validateObject(Object first, Object... objects) {
if (objects != null && objects.length > 0) {
return validateList(Lists.asList(first, objects));
} else if(first instanceof Collection){
return validateList((Collection)first);
}else{
return validate(first, new Class[0]);//这个Class[] x = new Class[0];x里面没有东西
}
}

//最终方法:可以检验任何
public static void check(Object param) throws ParamException {//BeanValidator->controller->handlerExcetionResolver
//异常层层向上抛出
Map<String, String> map = BeanValidator.validateObject(param);
if (MapUtils.isNotEmpty(map)) {
throw new ParamException(map.toString());//自定义异常向上抛出
        //日志打印等后续工作......
}
}


}











































































以上是关于validate的主要内容,如果未能解决你的问题,请参考以下文章