参数验证工具

简介: 参数验证工具

引言

参数校验始终不会少,但是如何让代码更加简洁?令人深思

接下来介绍一个参数验证工具

实现

注解

定义一个自定义注解

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定义

判断


相关文章
|
9月前
|
前端开发 算法 安全
轻松愉悦的验证方式:实现图片旋转验证功能
轻松愉悦的验证方式:实现图片旋转验证功能
226 0
|
1月前
|
安全 数据库 数据安全/隐私保护
处理用户输入数据格式验证不通过的情况时,如何给出友好的提示信息?
处理用户输入数据格式验证不通过的情况时,如何给出友好的提示信息?
154 78
|
9月前
|
数据采集 存储 安全
数据功能验证
数据功能验证
134 4
|
网络协议 算法 前端开发
记一次测试中对请求验证的处理
记一次测试中对请求验证的处理
|
测试技术
loadrunner 脚本开发-参数化之将内容保存为参数、参数数组及参数值获取
loadrunner 脚本开发-参数化之将内容保存为参数、参数数组及参数值获取
145 0
083.验证歌德巴赫猜想
083.验证歌德巴赫猜想
99 0
|
前端开发
前端工作小结73-进行输入成功后验证
前端工作小结73-进行输入成功后验证
79 0
|
监控 网络协议 机器人
验证 Googlebot (检查是否为真的Google机器人)
您可以验证访问您服务器的网页抓取工具是否确实是 Googlebot(还是其他 Google 用户代理)。如果您担心自称是 Googlebot 的垃圾内容发布者或其他麻烦制造者访问您的网站,则会发现该方法非常有用。
1810 0
vs2013在使用ef6时,创建模型向导过程中,四种模型方式缺少2种
下载eftool,并安装 https://www.microsoft.com/en-us/download/confirmation.aspx?id=40762 博客园大道至简 http://www.cnblogs.com/jams742003/ 转载请注明:博客园
1043 0
|
开发工具
【esayui】扩展验证方法,控件验证
基础验证 //页面调用方法$.extend($.fn.validatebox.defaults.rules, { 验证电话 IsPhoneRex: {validator: function (value) {var rex = /^1[3-8]+\d{9}$/;var rex2 = /^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;if (rex.
1042 0