• 自定义注解:实现枚举校验,组合条件校验


    判空注解

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface NullCheckValidate {
    
        String message() default "字段不能为空";
    }

    枚举注解

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface EnumCheckValidate {
    
        Class enumClass();
    
        String message() default "类型不匹配";
    
    }

    枚举继承接口

    public interface EnumInterface {
    
        String getEnumObj();
    }

    组合条件校验

    @Target(ElementType.FIELD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface GroupCheckValidate {
        /**
         * 当requst.get(key)=values时,验证
         */
    
        String key();
    
        String value();
    
        boolean isList();
    
        boolean isObject();
    
        String message() default "类型不匹配";
    
    }

    实现

        public static CommonResultResp validatorCheck(Object request) {
            CommonResultResp result = new CommonResultResp();
    
            Class c = request.getClass();
            Field[] fields = c.getDeclaredFields();
            for (Field fie : fields) {
                try {
                    fie.setAccessible(true);
                    /**
                     * 非空校验
                     */
                    NullCheckValidate nullCheckValidate = fie.getAnnotation(NullCheckValidate.class);
                    if (nullCheckValidate != null) {
                        if (StringUtils.isEmpty(fie.get(request))) {
                            result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL);
                            result.setResultMsg(nullCheckValidate.message());
                            return result;
                        }
                        continue;
                    }
    
                    /**
                     * 组合校验
                     */
                    GroupCheckValidate groupCheckValidate = fie.getAnnotation(GroupCheckValidate.class);
                    if (groupCheckValidate != null) {
                        Field value = c.getDeclaredField(groupCheckValidate.key());
                        value.setAccessible(true);
                        if (value == null || StringUtils.isEmpty(value.get(request))) {
                            value.setAccessible(false);
                            result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL);
                            result.setResultMsg(groupCheckValidate.message());
                            return result;
                        }
                        boolean groupV = checkByGroup(fie, request, value.get(request).toString(), groupCheckValidate);
                        value.setAccessible(false);
                        if (!groupV) {
                            result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL);
                            result.setResultMsg(groupCheckValidate.message());
                            return result;
                        }
                    }
    
                    /**
                     * 枚举校验
                     */
                    EnumCheckValidate enumCheckValidate = fie.getAnnotation(EnumCheckValidate.class);
                    if (enumCheckValidate != null) {
                        boolean enumV = checkByEnum(fie, request, enumCheckValidate.enumClass().getName());
                        if (!enumV) {
                            result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL);
                            result.setResultMsg(enumCheckValidate.message());
                            return result;
                        }
                        continue;
                    }
                    fie.setAccessible(false);
                } catch (Exception e) {
                    fie.setAccessible(false);
                    e.printStackTrace();
                    result.setResultCode(OmsCommonConsts.RESULE_CODE_FAIL);
                    result.setResultMsg("参数校验解析解析异常 code=" + fie.getName());
                    return result;
                }
            }
            result.setResultCode(OmsCommonConsts.RESULE_CODE_SUCCESS);
            result.setResultMsg("参数校验通过");
            return result;
    
        }
    
        private static boolean checkByEnum(Field field, Object request, String className) throws Exception {
            if (StringUtils.isEmpty(field.get(request))) {
                return false;
            }
            Class<?> onwClass = null;
            String value = field.get(request).toString();
            onwClass = Class.forName(className);
            Method method = onwClass.getMethod("values");
            EnumInterface[] inter = (EnumInterface[]) method.invoke(null);
            for (EnumInterface enumMessage : inter) {
                String object = enumMessage.getEnumObj();
                if (value.equals(object)) {
                    return true;
                }
            }
            return false;
        }
    
        private static boolean checkByGroup(Field field, Object request, String paramsValue, GroupCheckValidate groupCheckValidate) throws Exception {
    
            if (!paramsValue.equals(groupCheckValidate.value())) {
                return true;
            }
            Object o = field.get(request);
            if (groupCheckValidate.isList()) {
                if (CollectionUtils.isEmpty((Collection<?>) o)) {
                    return false;
                }
            } else {
                if (o == null) {
                    return false;
                }
            }
    
            return true;
        }

    需要判断的类

    /**
         * 购买方式
         */
        @ApiModelProperty(value = "【必填】下单类型:购物车:GWCGM,立即购买:LJGM")
      @NullCheckValidate    
    private String sourceType;
        @ApiModelProperty(value = "【立即购买必填】商品列表")
        @GroupCheckValidate(key = "sourceType", value = "LJGM", isList = true, isObject = false, message = "goodsItem 立即购买必填")
        private List<OrderGoodsItemDTO> goodsItem;
    
        /**
         * 物流信息
         */
        @ApiModelProperty(value = "【必填】 配发方式")
        @EnumCheckValidate(enumClass = OrderShipType.class, message = "shipType 类型不匹配,请参考文档")
        private String shipType;

    调用

     /**
             * 参数校验
             */
            CommonResultResp resp = Utils.validatorCheck(request);
            if (!resp.isSuccess()) {
                resp.setResultCode(resp.getResultCode());
                resp.setResultMsg(resp.getResultMsg());
                return resp;
            }
  • 相关阅读:
    用ildasm和ilasm对.net下的exe程序进行破解初探
    随笔--各种概念等等
    C#高级编程学习一-----------------第五章泛型
    visual studio相关操作
    webservice开发
    delphi各种错
    android开发文章收藏
    android开发遇到的问题
    android应用开发基础知道
    数据库各种问题
  • 原文地址:https://www.cnblogs.com/g-sheng/p/10277544.html
Copyright © 2020-2023  润新知