SpringWebMVC解決HttpServletRequest只能解析一次請求體,過濾器使用後請求進不到Controller中

問題描述:

過濾器中,解析請求體後,進入Controller後,報異常:Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Stream closed],

 

原因:

從HttpServletRequest中獲取到InputStream後,就不能再次獲取請求體

 

解決方法:

        參考鏈接:https://blog.csdn.net/lanxing_huangyao/article/details/86687851

        或者參考:com.mzqh.filter.TokenFilter

    

    思路:

            1、繼承 HttpServletRequestWrapper自定義請求Wrapper

            2、解析請求體,同時其設置到流 ServletInputStream 中

            3、過濾器中 方法 doFilter 中的request設置爲 HttpServletRequestWrapper 繼續傳遞流即可

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章