工具类:
public class ValidationUtils {
private static final Validator validator;
static {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
/**
* 校验对象
*
* @param object 待校验对象
* @param groups 待校验的组
* @throws BizException 校验不通过,则报业务异常
*/
public static void validateEntity(Object object, Class<?>... groups) throws IllegalArgumentException {
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
if (!constraintViolations.isEmpty()) {
String msg = constraintViolations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("||"));
throw new IllegalArgumentException(msg);
}
}
}
使用工具类
标签:constraintViolations,service,object,校验,Valid,static,groups,Validated,validator From: https://www.cnblogs.com/KL2016/p/18194419