随笔 - 330,  文章 - 1,  评论 - 0,  阅读 - 9025

引言

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

实现

注解

定义一个自定义注解

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

在这里插入图片描述

判断

posted on   vow007  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示