1. 程式人生 > 其它 >springboot request 只能讀取一次

springboot request 只能讀取一次

一,工具類

HttpUtil

package com.tenyears.common.utils;
 
import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
/**
 * @description :
 * @auther Tyler
 * @date 2021/8/31
 
*/ public class HttpUtil { /** * 獲取請求Body * * @param request * @return */ public static String getBodyString(ServletRequest request) { StringBuilder sb = new StringBuilder(); InputStream inputStream = null; BufferedReader reader = null;
try { inputStream = request.getInputStream(); reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } }
catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } }

二,BodyReaderHttpServletRequestWrapper 擴充套件類

package com.tenyears.extend;
 
import com.tenyears.common.utils.HttpUtil;
 
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
 
/**
 * @description :
 * @auther Tyler
 * @date 2021/8/31
 */
 
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
 
    private final byte[] body;
 
    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws Exception {
        super(request);
        body = HttpUtil.getBodyString(request).getBytes(Charset.forName("UTF-8"));
    }
 
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
 
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
 
        return new ServletInputStream() {
 
            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
 
            @Override
            public boolean isFinished() {
                return false;
            }
 
            @Override
            public boolean isReady() {
                return false;
            }
 
            @Override
            public void setReadListener(ReadListener readListener) {
 
            }
        };
    }
}

三,使用

@Autowired
HttpServletRequest request;
 
private void IdempotentCheck() throws Exception {
        // 防止流讀取一次後就沒有了, 因此須要將流繼續寫出去
        ServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
 
        String body = HttpUtil.getBodyString(requestWrapper);
        
 
    }