新聞中心
這篇文章主要介紹Spring AOP如何實現(xiàn)“切面式”valid校驗,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站長期為上1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為建湖企業(yè)提供專業(yè)的網(wǎng)站制作、網(wǎng)站建設(shè),建湖網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
why:
為什么要用aop實現(xiàn)校驗?
answer:
spring mvc 默認(rèn)自帶的校驗機(jī)制 @Valid + BindingResult, 但這種默認(rèn)實現(xiàn)都得在Controller方法的中去接收BindingResult,從而進(jìn)行校驗.
eg:
if (result.hasErrors()) { ListallErrors = result.getAllErrors(); List errorlists = new ArrayList<>(); for (ObjectError objectError : allErrors) { errorlists.add(objectError.getDefaultMessage()); } }
獲取errorlists。這樣實現(xiàn)的話,每個需要校驗的方法都得重復(fù)調(diào)用,即使封裝也是。
可能上面那么說還不能表明spring 的@Valid + BindingResult實現(xiàn),我先舉個“栗子”。
1. 栗子(舊版本)
1.1 接口層(IDAL)
eg: 簡單的POST請求,@RequestBody接收請求數(shù)據(jù),@Valid + BindingResult進(jìn)行校驗
httpMethid: POST
parameters:@RequestBody接收請求數(shù)據(jù)
valid:@Valid +BindingResult
@ResponseBody @PostMapping("body") public ResponseVO bodyPost(@RequestBody @Valid TestVO body,BindingResult result){ //校驗到錯誤 if (result.hasErrors()) { ListallErrors = result.getAllErrors(); List 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)校驗內(nèi)容
@Valid + BindingResult的校驗注解一大堆,網(wǎng)上一摸就有的!
public class TestVO { @Getter @Setter @Min(value = 0,message = "請求參數(shù)isString不能小于0") private Integer isInt; @Getter @Setter @NotBlank(message = "請求參數(shù)isString不能為空") private String isString; }
1.3 結(jié)果測試
2. aop校驗(升級版)
可以看到若是多個像bodyPost一樣都需要對body進(jìn)行校驗的話,那么有一坨代碼就必須不斷復(fù)現(xiàn),即使改為父類可復(fù)用方法,也得去調(diào)用。所以左思右想還是覺得不優(yōu)雅。所以有了aop進(jìn)行切面校驗。
2.1 接口層(IDAL)
是的!你沒看錯,上面那一坨代碼沒了,也不需要調(diào)用父類的的共用方法。就單單一個注解就完事了:@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 重點!切面實現(xiàn)(Aspect)
切面詳解:
@Before: 使用注解方式@annotation(XX),凡是使用到所需切的注解(@ParamValid),都會調(diào)用該方法
JoinPoint: 通過JoinPoint獲取方法的參數(shù),以此獲取BindingResult所校驗到的內(nèi)容
遷移校驗封裝: 將原先那一坨校驗遷移到Aspect中:validRequestParams
響應(yīng)校驗結(jié)果:
通過RequestContextHolder獲取response
獲取響應(yīng)OutputStream
將BindingResult封裝響應(yīng)
@Aspect @Component public 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 檢測到參數(shù)不規(guī)范" + 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()) { ListallErrors = result.getAllErrors(); List 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 測試結(jié)果
以上是“Spring AOP如何實現(xiàn)“切面式”valid校驗”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:SpringAOP如何實現(xiàn)“切面式”valid校驗
網(wǎng)址分享:http://www.ef60e0e.cn/article/jedcci.html