Java Validation Api實現(xiàn)原理解析
前言:
涉及知識點:AOP、攔截器相關(guān)
功能主要實現(xiàn)類:因為bean validation只提供了接口并未實現(xiàn),使用時需要加上一個provider的包,例如hibernate-validator
范圍: 注解:@Valid @RequestBudy
主要實現(xiàn)類:RequestResponseBodyMethodProcessor
處理器:HandlerMethodArgumentResolver
注解說明:
@Valid:標(biāo)準JSR-303規(guī)范的標(biāo)記型注解,用來標(biāo)記驗證屬性和方法返回值,進行級聯(lián)和遞歸校驗,@Valid可用于方法、字段、構(gòu)造器和參數(shù)上 @RequestBudy 請求的Body體,只能被讀取一次RequestResponseBodyMethodProcessor 類說明:
// @since 3.1public class RequestResponseBodyMethodProcessor extends AbstractMessageConverterMethodProcessor { @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(RequestBody.class); } // 類上或者方法上標(biāo)注了@ResponseBody注解都行 @Override public boolean supportsReturnType(MethodParameter returnType) { return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) || returnType.hasMethodAnnotation(ResponseBody.class)); } // 這是處理入?yún)⒎庋b校驗的入口,也是本文關(guān)注的焦點 @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { // 它是支持`Optional`容器的 parameter = parameter.nestedIfOptional(); // 使用消息轉(zhuǎn)換器HttpInputMessage把request請求轉(zhuǎn)換出來,拿到值~~~ // 此處注意:比如本例入?yún)⑹荘erson類,所以經(jīng)過這里處理會生成一個空的Person對象出來(反射) Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType()); // 獲取到入?yún)⒌拿Q,其實不叫形參名字,應(yīng)該叫objectName給校驗時用的 // 請注意:這里的名稱是類名首字母小寫,并不是你方法里寫的名字。比如本利若形參名寫為personAAA,但是name的值還是person // 但是注意:`parameter.getParameterName()`的值可是personAAA String name = Conventions.getVariableNameForParameter(parameter); // 只有存在binderFactory才會去完成自動的綁定、校驗~ // 此處web環(huán)境為:ServletRequestDataBinderFactory if (binderFactory != null) { WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name); // 顯然傳了參數(shù)才需要去綁定校驗嘛 if (arg != null) {// 這里完成數(shù)據(jù)綁定+數(shù)據(jù)校驗~~~~~(綁定的錯誤和校驗的錯誤都會放進Errors里)// Applicable:適合validateIfApplicable(binder, parameter);// 若有錯誤消息hasErrors(),并且僅跟著的一個參數(shù)不是Errors類型,Spring MVC會主動給你拋出MethodArgumentNotValidException異常// 否則,調(diào)用者自行處理if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());} } // 把錯誤消息放進去 證明已經(jīng)校驗出錯誤了~~~ // 后續(xù)邏輯會判斷MODEL_KEY_PREFIX這個key的~~~~ if (mavContainer != null) {mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult()); } } return adaptArgumentIfNecessary(arg, parameter); } // 校驗,如果合適的話。使用WebDataBinder,失敗信息最終也都是放在它身上~ 本方法是本文關(guān)注的焦點 // 入?yún)ⅲ篗ethodParameter parameter protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) { // 拿到標(biāo)注在此參數(shù)上的所有注解們(比如此處有@Valid和@RequestBody兩個注解) Annotation[] annotations = parameter.getParameterAnnotations(); for (Annotation ann : annotations) { // 先看看有木有@Validated Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class); // 這個里的判斷是關(guān)鍵:可以看到標(biāo)注了@Validated注解 或者注解名是以Valid打頭的 都會有效哦 //注意:這里可沒說必須是@Valid注解。實際上你自定義注解,名稱只要一Valid開頭都成~~~~~ if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith('Valid')) {// 拿到分組group后,調(diào)用binder的validate()進行校驗~~~~// 可以看到:拿到一個合適的注解后,立馬就break了~~~// 所以若你兩個主機都標(biāo)注@Validated和@Valid,效果是一樣滴~Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});binder.validate(validationHints);break; } } } ...}
可以看得,這個類應(yīng)該是陌生的,它能夠處理@ResponseBody注解返回值;它還有另一個能力是:它能夠處理請求參數(shù)(當(dāng)然也是標(biāo)注了@RequestBody的JavaBean)所以它既是個處理返回值的HandlerMethodReturnValueHandler,又是一個處理入?yún)⒌腍andlerMethodArgumentResolver。所以它命名為Processor而不是Resolver/Handler。
這是使用@RequestBody結(jié)合@Valid完成數(shù)據(jù)校驗的基本原理。其實當(dāng)Spring MVC在處理@RequestPart注解入?yún)?shù)據(jù)時,也會執(zhí)行綁定、校驗的相關(guān)邏輯。對應(yīng)處理器是RequestPartMethodArgumentResolver,原理大體上和這相似,它主要處理Multipart相關(guān),本文忽略~
以上就是dui’y對于@Valid標(biāo)注的@RequestBody的JavaBean的原理說明,敬請指點。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實戰(zhàn)》筆記--漸變設(shè)計(一)4. 移動端HTML5實現(xiàn)拍照功能的兩種方法5. 測試模式 - XSL教程 - 56. ASP.NET Core自定義中間件的方式詳解7. html5手機觸屏touch事件介紹8. CSS3實現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. 教你JS更簡單的獲取表單中數(shù)據(jù)(formdata)
