您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關如何解決spring 處理request.getInputStream()輸入流只能讀取一次問題,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一般我們會在InterceptorAdapter攔截器中對請求進行驗證
正常普通接口請求,request.getParameter()可以獲取,能多次讀取
如果我們的接口是用@RequestBody來接受數據,那么我們在攔截器中
需要讀取request的輸入流 ,因為 ServletRequest中getReader()和getInputStream()只能調用一次
這樣就會導致controller 無法拿到數據。
解決方法 :
1、自定義一個類 BodyReaderHttpServletRequestWrapper.java
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.springframework.util.StreamUtils; /** * @author WBG * @date 2020/6/22 10:42 * @describe */ public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper{ private byte[] requestBody = null;//用于將流保存下來 public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException { super(request); requestBody = StreamUtils.copyToByteArray(request.getInputStream()); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody); return new ServletInputStream() { @Override public int read() throws IOException { return bais.read(); } @Override public boolean isFinished() { return false; } @Override public boolean isReady() { return false; } @Override public void setReadListener(ReadListener readListener) { } }; } @Override public BufferedReader getReader() throws IOException{ return new BufferedReader(new InputStreamReader(getInputStream())); } }
2、自定義 MyFilter 繼承Filter
import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.BufferedReader; import java.io.IOException; /** * @author WBG * @date 2020/6/22 14:32 * @describe */ public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("開始"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { ServletRequest requestWrapper=null; if(request instanceof HttpServletRequest) { requestWrapper=new BodyReaderHttpServletRequestWrapper((HttpServletRequest)request); } if(requestWrapper==null) { chain.doFilter(request, response); }else { System.out.println("------------------------------請求報文----------------------------------"); System.out.println(getParamsFromRequestBody((HttpServletRequest) requestWrapper)); System.out.println("------------------------------請求報文----------------------------------"); chain.doFilter(requestWrapper, response); } } /* * * 獲取請求體內容 * @return * @throws IOException */ private String getParamsFromRequestBody(HttpServletRequest request) throws IOException { BufferedReader br = null; String listString = ""; try { br = request.getReader(); String str = ""; while ((str = br.readLine()) != null) { listString += str; } } catch (IOException e) { e.printStackTrace(); } return listString; } @Override public void destroy() { System.out.println("destroy"); } }
3、web.xm配置過濾器
<!--定義過濾器--> <filter> <!--定義過濾器攔截URL地址--> <filter-name>test2</filter-name> <!--過濾器的文件--> <filter-class>com.zhhy.hy2000interface.utils.filter.MyFilter</filter-class> </filter> <filter-mapping> <!--過濾器的名稱--> <filter-name>test2</filter-name> <!--過濾器負責攔截的URL--> <!-- /* 會把所有的請求攔截下來 --> <url-pattern>/*</url-pattern> </filter-mapping>
(如果是Springboot,使用注解即可)
以上就是如何解決spring 處理request.getInputStream()輸入流只能讀取一次問題,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。