why:
為什么要用aop實現校驗?
answer:
spring mvc 默認自帶的校驗機制 @Valid + BindingResult, 但這種默認實現都得在Controller方法的中去接收BindingResult,從而進行校驗.
eg:
if (result.hasErrors()) { List<ObjectError> allErrors = result.getAllErrors(); List<String> errorlists = new ArrayList<>(); for (ObjectError objectError : allErrors) { errorlists.add(objectError.getDefaultMessage()); } }
獲取errorlists。這樣實現的話,每個需要校驗的方法都得重復調用,即使封裝也是。
可能上面那么說還不能表明spring 的@Valid + BindingResult實現,我先舉個“栗子”。
1. 栗子(舊版本)
1.1 接口層(IDAL)
eg: 簡單的POST請求,@RequestBody接收請求數據,@Valid + BindingResult進行校驗
@ResponseBody @PostMapping("body") public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){ //校驗到錯誤 if (result.hasErrors()) { List<ObjectError> allErrors = result.getAllErrors(); List<String> lists = new ArrayList<>(); for (ObjectError objectError : allErrors) { lists.add(objectError.getDefaultMessage()); } return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists); } return new ResponseVO(HttpStatus.OK.value(), "bodyPost", null);}
1.2 實體(vo)校驗內容
@Valid + BindingResult的校驗注解一大堆,網上一摸就有的!
public class TestVO { @Getter @Setter @Min(value = 0,message = "請求參數isString不能小于0") private Integer isInt; @Getter @Setter @NotBlank(message = "請求參數isString不能為空") private String isString;}
1.3 結果測試
2. aop校驗(升級版)
可以看到若是多個像bodyPost一樣都需要對body進行校驗的話,那么有一坨代碼就必須不斷復現,即使改為父類可復用方法,也得去調用。所以左思右想還是覺得不優雅。所以有了aop進行切面校驗。
2.1 接口層(IDAL)
是的!你沒看錯,上面那一坨代碼沒了,也不需要調用父類的的共用方法。就單單一個注解就完事了:@ParamValid
@ParamValid@ResponseBody@PostMapping("body")public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){ return new ResponseVO("bodyPost", null);}
2.2 自定義注解(annotation)
這個注解也是簡簡單單的用于方法的注解。
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface ParamValid {}
2.3 重點!切面實現(Aspect)
切面詳解:
@Before: 使用注解方式@annotation(XX),凡是使用到所需切的注解(@ParamValid),都會調用該方法
JoinPoint: 通過JoinPoint獲取方法的參數,以此獲取BindingResult所校驗到的內容
遷移校驗封裝: 將原先那一坨校驗遷移到Aspect中:validRequestParams
響應校驗結果:
@Aspect@Componentpublic class ParamValidAspect { private static final Logger log = LoggerFactory.getLogger(ParamValidAspect.class); @Before("@annotation(paramValid)") public void paramValid(JoinPoint point, ParamValid paramValid) { Object[] paramObj = point.getArgs(); if (paramObj.length > 0) { if (paramObj[1] instanceof BindingResult) { BindingResult result = (BindingResult) paramObj[1]; ResponseVO errorMap = this.validRequestParams(result); if (errorMap != null) { ServletRequestAttributes res = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletResponse response = res.getResponse(); response.setCharacterEncoding("UTF-8"); response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); response.setStatus(HttpStatus.BAD_REQUEST.value()); OutputStream output = null; try { output = response.getOutputStream(); errorMap.setCode(null); String error = new Gson().toJson(errorMap); log.info("aop 檢測到參數不規范" + error); output.write(error.getBytes("UTF-8")); } catch (IOException e) { log.error(e.getMessage()); } finally { try { if (output != null) { output.close(); } } catch (IOException e) { log.error(e.getMessage()); } } } } } } /** * 校驗 */ private ResponseVO validRequestParams(BindingResult result) { if (result.hasErrors()) { List<ObjectError> allErrors = result.getAllErrors(); List<String> lists = new ArrayList<>(); for (ObjectError objectError : allErrors) { lists.add(objectError.getDefaultMessage()); } return new ResponseVO(HttpStatus.BAD_REQUEST.value(), "parameter empty", lists); } return null; }}
2.4 測試結果
看了上面兩種結果,就可以對比出使用Spring AOP 配合@Valid + BindingResult進行校驗的優點:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選