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);
… 然后处理业务逻辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值