com.zyy.common.util.ParamUtil Maven / Gradle / Ivy
package com.zyy.common.util;
import com.zyy.common.exception.ParamException;
import org.springframework.util.CollectionUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.groups.Default;
import java.util.Iterator;
import java.util.Set;
/**
* 参数校验工具类
*
* @author li.yang
*/
public class ParamUtil {
/**
* 判断参数正确性
*
* @param model 实体类
* @param 泛型
* @throws ParamException 自定义错误
*/
public static void validate(T model) throws ParamException {
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set> set = validator.validate(model, Default.class);
if (CollectionUtils.isEmpty(set)) {
Iterator> it = set.iterator();
if (it.hasNext()) {
throw new ParamException(it.next().getMessage());
}
}
}
}