怎么在SpringMVC中攔截Body參數(shù)

怎么在SpringMVC中攔截Body參數(shù)?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)公司服務(wù)項目包括大箐山網(wǎng)站建設(shè)、大箐山網(wǎng)站制作、大箐山網(wǎng)頁制作以及大箐山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,大箐山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到大箐山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

入?yún)?/strong>

RequestBodyAdvice : 針對所有以@RequestBody的參數(shù)做處理

參考案例 : JsonViewRequestBodyAdvice

public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter {

  /**
   * 這里是一個前置攔截匹配操作,其實就是告訴你滿足為true的才會執(zhí)行下面的beforeBodyRead方法,這里可以定義自己業(yè)務(wù)相關(guān)的攔截匹配
   * @param methodParameter
   * @param targetType
   * @param converterType
   * @return
   */
  @Override
  public boolean supports(MethodParameter methodParameter, Type targetType,
      Class<? extends HttpMessageConverter<?>> converterType) {
        
    return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&
        methodParameter.getParameterAnnotation(JsonView.class) != null);
  }

    // 這里就是具體的前置操作了... 下面的例子就是查找這個入?yún)⒎椒ㄊ欠裼蠤JsonView修飾
  @Override
  public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,
      Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

    JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);
    Class<?>[] classes = annotation.value();
    if (classes.length != 1) {
      throw new IllegalArgumentException(
          "@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
    }
    return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
  }
}

出參

ResponseBodyAdvice: 針對所有以@ResponseBody的參數(shù)做處理

參考案例:

@ControllerAdvice
public class LogResponseBodyAdvice implements ResponseBodyAdvice {
 
  /**
   *
   * @param returnType
   * @param converterType
   * @return
   */
  @Override
  public boolean supports(MethodParameter returnType, Class converterType) {
    return true;
  }

  @Override
  public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    // 做任何事情 body 就是返回的結(jié)果對象,沒有處理之前
    return body; 
  }
}

注意事項

自定義的處理對象類上必須得加上@ControllerAdvice注解!

為什么?

源碼中RequestMappingHandlerAdapter類在執(zhí)行initControllerAdviceCache()做初始化的時候會執(zhí)行一個

List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());
AnnotationAwareOrderComparator.sort(beans);

而ControllerAdviceBean.findAnnotatedBeans方法會查找類上有ControllerAdvice注解的類才會加入到處理當(dāng)中..

public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {
    List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();
    for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {
      if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {
        beans.add(new ControllerAdviceBean(name, applicationContext));
      }
    }
    return beans;
  }

看完上述內(nèi)容,你們掌握怎么在SpringMVC中攔截Body參數(shù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當(dāng)前文章:怎么在SpringMVC中攔截Body參數(shù)
分享鏈接:http://muchs.cn/article36/jopgsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、網(wǎng)站維護(hù)、網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)