zhazhahui

导航

统计

使用注解对参数进行校验

为什么需要参数进行校验

在很多的时候,开发者预想的参数与用户期望的参数可能是不一致的。比如,程序期望用户输入电话号码,但是用户给了一段日期,在没有参数校验的时候,这个日期参数就代替原本的手机号码,使得程序收集到脏数据,对用户体验也很不好。这个时候,需要有参数校验使得输入的参数符合程序的预期,同时也给用户反馈,告知我们需要什么参数。

设计思路

使用切片注入的方式,在业务逻辑执行前,对参数进行校验。

具体如下

拦截所有请求

@Before("@annotation(org.springframework.web.bind.annotation.GetMapping)||" +
        "@annotation(org.springframework.web.bind.annotation.PostMapping)||" +
        "@annotation(org.springframework.web.bind.annotation.PutMapping)||" +
        "@annotation(org.springframework.web.bind.annotation.DeleteMapping)")

获取切入点的所有方法和注解

MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
LOGGER.debug("Starting validate method {}.", method.getName());
Annotation[][] annotations = method.getParameterAnnotations();
Object[] parameters = point.getArgs();

对所有带Validated注解的入参进行校验

for (int i = 0; i < annotations.length; i++) {
    for (int j = 0; j < annotations[i].length; j++) {
        if (annotations[i][j] instanceof Validated) {
            validateArg(parameters[i]);
        }
    }
}

对参数的所有变量进行校验

private void validateArg(Object arg) {
	//获取所有的成员变量
    Field[] fields = arg.getClass().getDeclaredFields();
    for (Field field : fields) {
       //获取所有实现Validator接口的bean,bean为具体注解的逻辑实现
       Map<String, Validator> validatorMap = context.getBeansOfType(Validator.class);
        for (Validator validator : validatorMap.values()) {
            //如果成员变量有注解,将对成员变量进行参数校验
            if (validator.support(field)) {
                validator.handle(arg, field);
            }
        }
    }
}

posted on   zhazhahui  阅读(204)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示