美文网首页
参数非空校验

参数非空校验

作者: Tinyspot | 来源:发表于2024-01-07 20:38 被阅读0次

    1. 示例

    @Test
    public void paramCheck() {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderCode("");
    
        ParamCheckUtils.checkParamNotBlank(orderDTO.getOrderCode(), "is_blank");
    
        ResultDTO blank = ReParamCheckUtils.checkParamNotBlank(orderDTO.getOrderCode(), "is_blank");
        ResultDTO blank2 = ReParamCheckUtils.checkParamNotBlank(new Object[] {orderDTO.getOrderCode()}, "is_blank");
        System.out.println(blank);
    }
    

    2. 工具类

    ParamCheckUtils 直接抛异常

    public class ParamCheckUtils {
    
        public static void checkParamNotBlank(Object arg, String errorDesc) {
            checkParamsNotBlank(new Object[]{arg}, errorDesc);
        }
    
        public static void checkParamsNotBlank(Object[] args, String errorDesc) {
            if (args.length > 0) {
                for (Object object : args) {
                    if (object == null) {
                        throw new BizRuntimeException(false, "", errorDesc);
                    } else if (object instanceof String) {
                        if (StringUtils.isBlank((String) object)) {
                            throw new BizRuntimeException(false, "", errorDesc);
                        }
                    } else if (object instanceof List<?>) {
                        if (CollectionUtils.isEmpty((List<?>) object)) {
                            throw new BizRuntimeException(false, "", errorDesc);
                        }
                    }
                }
            }
        }
    }
    

    ReParamCheckUtils 封装返回值

    public class ReParamCheckUtils {
    
        public static ResultDTO checkParamNotBlank(Object arg, String errorDesc) {
            return checkParamNotBlank(new Object[]{arg}, errorDesc);
        }
    
        // public static ResultDTO checkParamNotBlank(String errDesc, Object ...args) {
        //     return checkParamNotBlank(args, errDesc);
        // }
    
        public static ResultDTO checkParamNotBlank(Object[] args, String errDesc) {
            if (args.length > 0) {
                for (Object object : args) {
                    if (object == null) {
                        return ResultDTO.fail("PARAM_NULL", "PARAM_NULL");
                    } else if (object instanceof String) {
                        if (StringUtils.isBlank((String) object)) {
                            return ResultDTO.fail("PARAM_NULL", "PARAM_NULL");
                        }
                    } else if (object instanceof Collection<?>) {
                        if (CollectionUtils.isEmpty((Collection<?>) object)) {
                            return ResultDTO.fail("PARAM_NULL", "PARAM_NULL");
                        }
                    }
                }
            }
            return ResultDTO.newSuccessResult("success");
        }
    }
    

    相关文章

      网友评论

          本文标题:参数非空校验

          本文链接:https://www.haomeiwen.com/subject/bgxnndtx.html