springboot request 只能读取一次

Posted 正怒月神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        

    }

以上是关于springboot request 只能读取一次的主要内容,如果未能解决你的问题,请参考以下文章

web应用程序中解决Request和Response只能获取一次的问题

web应用程序中解决Request和Response只能获取一次的问题

SpringBoot 接口加密解密,新姿势!

springboot获取请求参数的几种方式可选参数使用

多次读取请求request里数据

解决HttpServletRequest的输入流只能读取一次的问题