public class ValidationUtil{
/**
* 工厂类
**/
private static ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
public statuc <T> void validate(T t){
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> violations = validator.validate(t);
ArrayList<String> list = new ArrayList<>();
for(ConstraintViolation<T> constraintVioation : violations){
list.add(constraintViolation.getMessage());
}
if(list.size() > 0){
throw new Exception("自定义报错");
}
}
}