背景

入参校验一般是在控制层通过javax.validation.constraints包下的规则注解如NotNull结合Valid与Validated实现,但是有时候我们的方法不提供给controller调用,这时候就无法触发自动参数校验,为此我们可以在不更改校验代码的前提下手动触发spring的参数校验。

代码实现

  • 触发工具类
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import java.util.List;
import java.util.Set;

/**
 * @description validator参数校验类
 */
public class ValidateUtil {

    private static final Validator VALIDATOR = Validation.buildDefaultValidatorFactory().getValidator();

    /**
     * 校验实体类
     * @param t  参数
     * @param notNull t是否可以为空
     */
    public static <T> void validate(T t, boolean notNull) {
        if (t == null) {
            if (notNull) {
                throw new ValidationException("参数不能为空");
            }
            return;
        }
        Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(t);
        if (constraintViolations.size() < 1) {
            return;
        }
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            throw new ValidationException(constraintViolation.getMessage());
        }
    }

    public static <T> void validate(T t) {
        validate(t, false);
    }

    public static <T> void validate(List<T> list) {
        validate(list, false);
    }

    /**
     * @description: 参数校验
     * @date 11:59 2024/6/7
     * @param list
     * @param notNull list是否可以为空
     **/
    public static <T> void validate(List<T> list, boolean notNull) {
        if (list == null || list.isEmpty()) {
            if (notNull) {
                throw new ValidationException("参数不能为空");
            }
            return;
        }

        list.forEach(item -> {
            Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(item);
            if (constraintViolations.size() < 1) {
                return;
            }
            for (ConstraintViolation<T> constraintViolation : constraintViolations) {
                throw new ValidationException(constraintViolation.getMessage());
            }
        });
    }

    /**
     * 通过组来校验实体类
     * @param t 参数
     * @param notNull t是否可以为空
     * @param groups 校验组
     */
    public static <T> void validate(T t, boolean notNull, Class<?>... groups) {
        if (t == null) {
            if (notNull) {
                throw new ValidationException("参数不能为空");
            }
            return;
        }
        Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(t, groups);
        if (constraintViolations.size() < 1) {
            return;
        }
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            throw new ValidationException(constraintViolation.getMessage());
        }
    }

    public static <T> void validate(T t, Class<?>... groups) {
        validate(t, false, groups);
    }
}
  • 使用
// 方法入参
public class RefundOrderParam {

    @ApiModelProperty(value = "总退款金额", required = true)
    @NotNull(message = "总退款金额不能为空", groups = {InsertGroup.class, UpdateGroup.class})
    private BigDecimal totalRefundMoney;

    @ApiModelProperty(value = "是否原路退", required = true)
    @NotNull(message = "是否原路退不能为空", groups = {InsertGroup.class, UpdateGroup.class})
    private Boolean originalRefund;
}
//触发校验
ValidateUtil.validate(request, UpdateGroup.class);

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部