io.github.shitsurei.common.util.ValidationUtil Maven / Gradle / Ivy
The newest version!
package io.github.shitsurei.common.util;
import io.github.shitsurei.dao.enumerate.system.GlobalExceptionEnum;
import io.github.shitsurei.dao.exception.GlobalException;
import org.springframework.util.CollectionUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashSet;
import java.util.Set;
/**
* 校验工具类
*
* @author zhanggr-b
* @version 1.0
* @date 2022/2/7 15:01
*/
public class ValidationUtil {
private static final ValidatorFactory VALIDATOR_FACTORY = Validation.buildDefaultValidatorFactory();
/**
* 校验DTO
* @param paramObject
*/
public static void validateObject(Object paramObject) {
Validator validator = VALIDATOR_FACTORY.getValidator();
Set> validateResult = validator.validate(paramObject);
if (CollectionUtils.isEmpty(validateResult)) {
return;
}
StringBuilder errorMessageSb = new StringBuilder();
for (ConstraintViolation