Java Validation Api實(shí)現(xiàn)原理解析
前言:
涉及知識點(diǎn):AOP、攔截器相關(guān)
功能主要實(shí)現(xiàn)類:因?yàn)閎ean validation只提供了接口并未實(shí)現(xiàn),使用時需要加上一個provider的包,例如hibernate-validator
范圍: 注解:@Valid @RequestBudy
主要實(shí)現(xiàn)類:RequestResponseBodyMethodProcessor
處理器:HandlerMethodArgumentResolver
注解說明:
@Valid:標(biāo)準(zhǔn)JSR-303規(guī)范的標(biāo)記型注解,用來標(biāo)記驗(yàn)證屬性和方法返回值,進(jìn)行級聯(lián)和遞歸校驗(yà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校驗(yàn)的入口,也是本文關(guān)注的焦點(diǎ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,其實(shí)不叫形參名字,應(yīng)該叫objectName給校驗(yàn)時用的 // 請注意:這里的名稱是類名首字母小寫,并不是你方法里寫的名字。比如本利若形參名寫為personAAA,但是name的值還是person // 但是注意:`parameter.getParameterName()`的值可是personAAA String name = Conventions.getVariableNameForParameter(parameter); // 只有存在binderFactory才會去完成自動的綁定、校驗(yàn)~ // 此處web環(huán)境為:ServletRequestDataBinderFactory if (binderFactory != null) { WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name); // 顯然傳了參數(shù)才需要去綁定校驗(yàn)嘛 if (arg != null) {// 這里完成數(shù)據(jù)綁定+數(shù)據(jù)校驗(yàn)~~~~~(綁定的錯誤和校驗(yàn)的錯誤都會放進(jìn)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ìn)去 證明已經(jīng)校驗(yàn)出錯誤了~~~ // 后續(xù)邏輯會判斷MODEL_KEY_PREFIX這個key的~~~~ if (mavContainer != null) {mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult()); } } return adaptArgumentIfNecessary(arg, parameter); } // 校驗(yàn),如果合適的話。使用WebDataBinder,失敗信息最終也都是放在它身上~ 本方法是本文關(guān)注的焦點(diǎ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注解。實(shí)際上你自定義注解,名稱只要一Valid開頭都成~~~~~ if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith('Valid')) {// 拿到分組group后,調(diào)用binder的validate()進(jìn)行校驗(yàn)~~~~// 可以看到:拿到一個合適的注解后,立馬就break了~~~// 所以若你兩個主機(jī)都標(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ù)校驗(yàn)的基本原理。其實(shí)當(dāng)Spring MVC在處理@RequestPart注解入?yún)?shù)據(jù)時,也會執(zhí)行綁定、校驗(yàn)的相關(guān)邏輯。對應(yīng)處理器是RequestPartMethodArgumentResolver,原理大體上和這相似,它主要處理Multipart相關(guān),本文忽略~
以上就是dui’y對于@Valid標(biāo)注的@RequestBody的JavaBean的原理說明,敬請指點(diǎn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. JSP之表單提交get和post的區(qū)別詳解及實(shí)例2. CSS可以做的幾個令你嘆為觀止的實(shí)例分享3. Xml簡介_動力節(jié)點(diǎn)Java學(xué)院整理4. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲5. javascript xml xsl取值及數(shù)據(jù)修改第1/2頁6. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案7. jsp文件下載功能實(shí)現(xiàn)代碼8. 將properties文件的配置設(shè)置為整個Web應(yīng)用的全局變量實(shí)現(xiàn)方法9. ASP常用日期格式化函數(shù) FormatDate()10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
