Java 属性校验工具类 ValidatorUtils

import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.ss.formula.functions.T;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
 * 用于校验类中属性的限制注解 如:@NotBlank(message = "出库申请单号不能为空")
 * @author zt
 * @date 2022/7/25 17:44
 */
public class ValidatorUtils  {

    private static final Validator validatorFast = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();
    private static final Validator validatorAll = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();

    /**
     * 校验字段 failFast
     * @param t 被校验对象
     */
    public static <T> void validateFast(T t) {
        validateFast(t, null);
    }

    /**
     * 校验字段 failFast
     * @param t 被校验对象
     * @param description 错误描述
     */
    public static <T> void validateFast(T t, String description) {
        Set<ConstraintViolation<T>> validateResult = validatorFast.validate(t);
        if (CollectionUtils.isNotEmpty(validateResult)){
            throw new BusinessException(Optional.ofNullable(description).orElse("errorMessage") + ": " + validateResult.stream().map(ConstraintViolation::getMessage).reduce((m1, m2) -> m1 + ";" + m2).orElse(""));
        }
    }

    /**
     * 校验字段 failFast
     * @param collection 被校验集合
     */
    public static <T> void validateFast(Collection<T> collection) {
        if (Objects.isNull(collection)){
            throw new IllegalArgumentException("The collection to be validated must not be null.");
        }
        for (T t : collection) {
            validateFast(t);
        }
    }

    /**
     * 校验字段
     * @param t 被校验对象
     */
    public static <T> void validateAll(T t) {
        validateAll(t, null);
    }

    /**
     * 校验字段
     * @param t 被校验对象
     * @param description 错误描述
     */
    public static <T> void validateAll(T t, String description) {
        Set<ConstraintViolation<T>> validateResult = validatorAll.validate(t);
        if(validateResult.size()>0) {
            throw new BusinessException(Optional.ofNullable(description).orElse("errorMessage") + ": " + validateResult.stream().map(ConstraintViolation::getMessage).reduce((m1, m2) -> m1 + ";" + m2).orElse(""));
        }
    }

    /**
     * 校验字段
     * @param collection
     */
    public static <T> void validateAll(Collection<T> collection) {
        if (Objects.isNull(collection)){
            throw new IllegalArgumentException("The collection to be validated must not be null.");
        }
        Set<ConstraintViolation<T>> validateResult = new HashSet<>();
        for (T t : collection) {
            validateResult.addAll(validatorAll.validate(t));
        }
        if (CollectionUtils.isNotEmpty(validateResult)) {
            throw new BusinessException("errorMessage" + ": " + validateResult.stream().map(ConstraintViolation::getMessage).reduce((m1, m2) -> m1 + ";" + m2).orElse(""));
        }
    }

}

用法:
@Override
@Transactional(rollbackFor = Exception.class)
public void accountReceivableReject(AccountReceivableRejectReqVo accountReceivableRejectReqVo) {
//首先校验参数是否为空
//统一校验参数长度
ValidatorUtils.validateFast(accountReceivableRejectReqVo);
… 然后处理业务逻辑

你可能感兴趣的:(Java,java)