validate with annotation

本文介绍了一种使用自定义注解进行HTTP请求参数验证的方法,包括定义注解、使用场景及处理逻辑。通过实例展示了如何确保传入参数的有效性和正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

setp
1:define
    @Retention(RetentionPolicy.RUNTIME)
    public @interface RequestValidate {
        RequestValidateItem[] value();
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface RequestValidateItem {
        public String parameter() default "";
        public static final String TYPE_DOUBLE = "#DOUBLE";
    }
2:use point
    @RequestValidate({
        @RequestValidateItem(parameter="createTime",required=true,caption="创建时间",msg="不能为空",type=RequestValidateItem.TYPE_DATE),
        @RequestValidateItem(parameter="endTime",required=true,caption="创建时间",msg="不能为空",type=RequestValidateItem.TYPE_DATE)
    })
3:proccess logic
            targetMethod = this.getClass().getMethod(targetMethodName);
            if (targetMethod.isAnnotationPresent(RequestValidate.class)) {
                StringBuffer retObjMsg = new StringBuffer();
                Annotation[] annotations = targetMethod.getAnnotations();
                for (Annotation annotation : annotations) {
                    if (annotation instanceof RequestValidate) {
                        RequestValidate rv = (RequestValidate) annotation;
                        for(RequestValidateItem rvi : rv.value()){
                            String retValidate = validate(rvi);//detail logic
                            if (null != retValidate) {
                                retObjMsg.append(retValidate + "   ");
                            }
                        }
                    }
                if (retObjMsg.length() > 0)
                    return createMsgBox(retObjMsg.toString());
            }
            retObj = targetMethod.invoke(this);


test method:use po test annotation
    1:new plugmodule:<property name="autoRegisterComponent" value="true"/>
    2:new DemoPO extends StatePresentationObject
    3:<service-using
            serviceUuid="hippo.plugmodule.services.presentation"
            description="注册必要的入口页面">
            <WebPresentation namespace="hkx"
                autoRegisterResource="true" autoRegisterPO="true"
                presentationObjectPackage="com.woaika.framework.test.hkx.prez">
            </WebPresentation>
    </service-using>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值