Validation工具类
工具类代码
package cn.com.taiji.approve.manager.vehicleimport;
import java.util.Iterator;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
/**
* @version:1.0.0
* @author: yangqs
* @date:2020-08-28
* @description: validator工具类
*/
public class BeanValidator {
//校验工厂
private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
/**
* @Description: 校验返回提示信息
* @param T
* @param groups
* @return 提示信息拼接字符串
*/
public static <T> String validate(T t, Class<?>... groups) {
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<T>> validate = validator.validate(t, groups);
if (validate.isEmpty()) {
return "";
} else {
StringBuffer sb = new StringBuffer();
Iterator<ConstraintViolation<T>> iterator = validate.iterator();
while (iterator.hasNext()) {
ConstraintViolation<T> violation = iterator.next();
sb.append(violation.getMessage()).append(";");
}
return sb.toString();
}
}
}