参数验证工具

引言

参数校验始终不会少,但是如何让代码更加简洁?令人深思
接下来介绍一个参数验证工具

实现

注解

定义一个自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 参数非空注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface NotEmpty {

    /**
     * 是否非空。<br>
     *
     * @return
     */
    boolean value() default true;

    /**
     * 错误信息描述 默认提示fieldName非空
     * @return
     */
    String message() default "";

}

判断工具

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

import com.alibaba.common.lang.StringUtil;
import com.alibaba.global.ad.core.client.annotation.NotEmpty;
import com.alibaba.global.ad.core.client.common.exception.ServiceException;
import com.alibaba.global.ad.core.client.common.result.ResultCode;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 断言检查工具类
 * 抽象,防止直接实例化
 *
 */
public class GenericCheckUtil {

    /**
     * 是有构造,防止被实现
     */
    private GenericCheckUtil() {
    }

    /**
     * 校验参数(推荐)
     *
     * @param param 不能为空
     */
    public static void check(Object param) {
        if (param == null) {
            throw new ServiceException(ResultCode.ILLEGAL_PARAM);
        }
        for (Field field : param.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            NotEmpty notEmpty = field.getAnnotation(NotEmpty.class);
            if (notEmpty == null || !notEmpty.value()) {
                continue;
            }
            Object value;
            try {
                value = field.get(param);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            // null判断
            if (value == null) {
                throwException(field, notEmpty);
            }
            // String判断
            if (value instanceof String && StringUtil.isBlank((String)value)) {
                throwException(field, notEmpty);
            }
            // 集合判断
            if (value instanceof Collection && CollectionUtils.isEmpty((Collection<?>)value)) {
                throwException(field, notEmpty);
            }
            if (value instanceof Map && CollectionUtils.isEmpty((Map)value)) {
                throwException(field, notEmpty);
            }
        }
    }

    /**
     * 抛出异常
     *
     * @param field
     * @param notEmpty
     */
    private static void throwException(Field field, NotEmpty notEmpty) {
        throw new ServiceException(ResultCode.ILLEGAL_PARAM,
            StringUtil.isBlank(notEmpty.message()) ? field.getName() + "不能为空" : notEmpty.message());
    }

}

使用

DTO定义

在这里插入图片描述

判断

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进朱者赤

多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值