内容简介:Spring AOP @PathVariable和@RequestParam 参数进行校验(valid)
在上一篇文https://juejin.im/post/5a5e1159518825732b19d8ce,通过AOP对@RequestBody注解进行的参数进行校验 那么对于 @PathVariable和@RequestParam 却没有对应的spring mvc 默认自带的校验机制 @Valid + BindingResult。那么此时该校验的话,只能代码上逐一进行校验。
先说明一下@PathVariable和@RequestParam;两个注解的用法。
1.原版本讲解
1.1 @PathVariable
1.1.1 RESTful风格
格式:path/1/catalpaFlat eg:
@GetMapping("path/{isInt}/{isString}") public ResponseVO pathGet(@PathVariable Integer isInt, @PathVariable String isString) { log.info("int:" + isInt); log.info("String:" + isString); JSONObject resultJson = new JSONObject(); resultJson.put("isInt", isInt); resultJson.put("isString", isString); return new ResponseVO(HttpStatus.OK.value(), "pathGet", resultJson); }
request: http://localhost:8888/path/3/dadas
1.1.2 校验
代码式校验
if(isInt < 2){ return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "pathGet", "isInt must be more than 2"); }
代码式校验的话,个人觉得就有点儿不必要,除非是一些特殊需求
1.2 @RequestParam
1.2.1 表单提交(query)
格式:query?isInt=2&isString=catalpaFlat
@GetMapping("query?") public ResponseVO queryGet(@RequestParam Integer isInt, @RequestParam String isString) { log.info("int:" + isInt); log.info("String:" + isString); JSONObject resultJson = new JSONObject(); resultJson.put("isInt", isInt); resultJson.put("isString", isString); return new ResponseVO(HttpStatus.OK.value(), "queryGet", resultJson); }
1.2.2 校验
同样也需要代码式校验
if(isInt < 2){ return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "queryGet", "isInt must be more than 2"); }
因为@Valid + BindingResult只能用于@ResponseBody这种类型注解。
2. AOP改良版
可以感受到原版本都得在代码中进行校验,只要使用到@PathVariable和@RequestParam的都得进行一波参数校验,因此就想能不能像@Valid一样,为其添加注解式校验,这样代码量就减少并让代码优雅了很多。
2.1 接口层(IDAL)
以下代码再则是改良之后的代码:
- @ParameterValid 相当于@Valid,并且在其属性中进行配置参数校验规则
- @PathAndQueryParamValid 相当于AOP的切入点
@PathAndQueryParamValid @GetMapping("path/{isInt}/{isString}") public ResponseVO pathGet(@PathVariable @ParameterValid(type = Integer.class, msg = "isInt must be more than 2", isMin = true, min = 2) Integer isInt, @PathVariable @ParameterValid(type = String.class, msg = "isString is empty") String isString) { log.info("int:" + isInt); log.info("String:" + isString); JSONObject resultJson = new JSONObject(); resultJson.put("isInt", isInt); resultJson.put("isString", isString); return new ResponseVO(HttpStatus.OK.value(), "pathGet", resultJson); } @GetMapping("query") @PathAndQueryParamValid public ResponseVO queryGet(@RequestParam @ParameterValid(type = Integer.class, msg = "isInt must be more than 2 ", isMin = true, min = 2) Integer isInt, @RequestParam @ParameterValid(type = String.class, msg = "isString is empty") String isString) { log.info("int:" + isInt); log.info("String:" + isString); JSONObject resultJson = new JSONObject(); resultJson.put("isInt", isInt); resultJson.put("isString", isString); return new ResponseVO(HttpStatus.OK.value(), "queryGet", resultJson); }
2.2 自定义注解(annotation)
2.2.1 @PathAndQueryParamValid
只是简单的用于方法类型注解:
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface PathAndQueryParamValid { }
2.2.1 @ParameterValid
@ParameterValid 可以根据实际业务需求添加属于你的校验规则:
@Documented @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) public @interface ParameterValid { Class<?> type(); String msg(); boolean request() default true; boolean isEmpty() default true; boolean isBlank() default true; boolean isNull() default false; int min() default 0; int max() default 0; int[] section() default {0,0}; boolean isMin() default false; boolean isMax() default false; boolean isSection() default false; }
2.3 AOP切面(重点-1)
- 通过joinPoint获取切点方法名以及类名,后续(重点)有大用
- 通过JoinPoint获取方法的参数
- 调用(重点2)ParamValidSupport
- AdvanceResponseSupport和上一篇文章内容一样,其实就是响应而已。
@Aspect @Component public class PathAndQueryParamValidAspect { private static final Logger log = LoggerFactory.getLogger(PathAndQueryParamValidAspect.class); @Before("@annotation(paramValid)") public void paramValid(JoinPoint joinPoint, PathAndQueryParamValid paramValid) { String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] param = joinPoint.getArgs(); try { List<String> errorLists = ParamValidSupport.get().validate(className, methodName, ParameterValid.class, param); if (errorLists != null) { AdvanceResponseSupport.advanceResponse( new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", errorLists)); } } catch (NotFoundException | NoSuchMethodException | ClassNotFoundException e) { log.error("e-name:" + e.getClass().getName() + ": message:" + e.getMessage()); } } }
2.4 校验(重点-2)
- 通过方法名和类名,根据ClassPool获取CtClass和CtMethod
- 从而获取参数注解,解析参数注解规则进行校验参数
(这里还可以重构的,只是对int和string两种类型进行校验,还可以添加其他需求。把主要内容先呈现出来)
public class ParamValidSupport { private static final Logger logger = LoggerFactory.getLogger(ParamValidSupport.class); private static final String PARAM_TYPE_ERROR = "param type error"; private static final String INT_PARAM_ERROR = "Invalid interva"; private static final int INT_PARAM_TYPE_MAX_SIZE = 2; private static final int INT_PARAM_SIZE_SUBSCRIPT_MIN = 0; private static final int INT_PARAM_SIZE_SUBSCRIPT_MAX = 0; private static final int STRING_SIZE = 2; private static final char STRING_TYPE_END = '}'; private static final char STRING_TYPE_BEGIN = '{'; private static final char STRING_EMPTY_DOUBLE_CHARACTER = '"'; private static final char STRING_EMPTY_SINGLE_CHARACTER = '\''; private static ParamValidSupport mInstance; private ParamValidSupport() { } public static ParamValidSupport get() { if (mInstance == null) { synchronized (ParamValidSupport.class) { if (mInstance == null) { mInstance = new ParamValidSupport(); } } } return mInstance; } /** * 校验 */ public List<String> validate(String className, String methodName, Class<?> annotationClass, Object[] args) throws NotFoundException, NoSuchMethodException, ClassNotFoundException { if (StringUtils.isBlank(className)) { return null; } if (StringUtils.isBlank(methodName)) { return null; } if (annotationClass == null) { return null; } ClassPool pool = ClassPool.getDefault(); CtClass ct = pool.get(className); CtMethod ctMethod = ct.getDeclaredMethod(methodName); Object[][] parameterAnnotations = ctMethod.getParameterAnnotations(); List<String> errorLists = new ArrayList<>(); for (int i = 0; i < parameterAnnotations.length; i++) { Object[] parameterAnnotation = parameterAnnotations[i]; Object param = args[i]; for (Object object : parameterAnnotation) { Annotation annotation = (Annotation) object; Class<? extends Annotation> aClass = annotation.annotationType(); if (aClass.equals(annotationClass)) { boolean isEmpty = ((ParameterValid) object).isEmpty(); if (isEmpty) { ParameterValid parameterValid = (ParameterValid) object; String errorMsg = parameterValid.msg(); if (Integer.class.isAssignableFrom(param.getClass())){ int paramInt = (int) param; if (parameterValid.isMin() && paramInt < parameterValid.min()) { errorLists.add(errorMsg); } if (parameterValid.isMax() && paramInt < parameterValid.max()) { errorLists.add(errorMsg); } if (parameterValid.isSection()) { int[] section = parameterValid.section(); if (section.length != INT_PARAM_TYPE_MAX_SIZE) { logger.error(INT_PARAM_ERROR); throw new ParameterValidException(INT_PARAM_ERROR); } if (!(paramInt > section[INT_PARAM_SIZE_SUBSCRIPT_MIN] && paramInt < section[INT_PARAM_SIZE_SUBSCRIPT_MAX])) { errorLists.add(errorMsg); } else if (!(paramInt > section[INT_PARAM_SIZE_SUBSCRIPT_MAX] && paramInt < section[INT_PARAM_SIZE_SUBSCRIPT_MIN])) { errorLists.add(errorMsg); } } } if (String.class.isAssignableFrom(param.getClass())){ String paramStr = (String) param; if (parameterValid.isNull()) { if (StringUtils.isEmpty(paramStr)) { errorLists.add(errorMsg); } } else { if (parameterValid.isBlank()) { if (StringUtils.isBlank(paramStr)) { errorLists.add(errorMsg); } else { int length = paramStr.length(); char begin = paramStr.charAt(0); char end = paramStr.charAt(length - 1); if (STRING_TYPE_BEGIN == begin && STRING_TYPE_END == end) { errorLists.add(errorMsg); } if (length == STRING_SIZE && STRING_EMPTY_DOUBLE_CHARACTER == begin && STRING_EMPTY_DOUBLE_CHARACTER == end) { errorLists.add(errorMsg); } if (length == STRING_SIZE && STRING_EMPTY_SINGLE_CHARACTER == begin && STRING_EMPTY_SINGLE_CHARACTER == end) { errorLists.add(errorMsg); } } } } } } } } } if (errorLists.size() != 0) { return errorLists; } return null; } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 更加灵活的参数校验,Spring-boot自定义参数校验注解
- 一坨一坨的 if/else 参数校验,终于被 Spring Boot 参数校验组件整干净了
- gin - validator 参数校验
- 【快学springboot】4.接口参数校验
- Spring校验@RequestParams和@PathVariables参数
- SpringBoot 实战 (十五) | 服务端参数校验之一
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
数据结构与算法经典问题解析
纳拉辛哈·卡鲁曼希 / 骆嘉伟 / 机械工业出版社 / 2016-6-1 / CNY 79.00
本书是一本数据结构方面的优秀教材,以Java为描述语言,介绍了计算机编程中使用的数据结构和算法。本书强调问题及其分析,而非理论阐述,共分为21章,讲述了基本概念、递归和回溯、链表、栈、队列、树、优先队列和堆、并查集DAT、图算法、排序、查找、选择算法(中位数)、符号表、散列、字符串算法、算法设计技术、贪婪算法、分治算法、动态规划算法、复杂度类型等内容。每章首先阐述必要的理论基础,然后给出问题集。全......一起来看看 《数据结构与算法经典问题解析》 这本书的介绍吧!