Java 如何在Spring Boot中获得完整的HttpServletResponse主体?

Java 如何在Spring Boot中获得完整的HttpServletResponse主体?,java,spring,Java,Spring,我想获取HttpServletResponse返回内容,用于登录自定义拦截器。开发环境为Spring Boot 1.5.6+Java 8+嵌入式Tomcat 8.0.35,返回内容为RESTful接口json字符串。这是我获取http响应内容的代码: /** * get Response return json content * * @param response * @return * @throws IOException * @throws NoSuchFieldExcep

我想获取HttpServletResponse返回内容,用于登录自定义拦截器。开发环境为Spring Boot 1.5.6+Java 8+嵌入式Tomcat 8.0.35,返回内容为RESTful接口json字符串。这是我获取http响应内容的代码:

 /**
 * get Response return json content
 *
 * @param response
 * @return
 * @throws IOException
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
public String getResponseContent(HttpServletResponse response) throws IOException, NoSuchFieldException, IllegalAccessException {
    String responseContent = null;
    CoyoteOutputStream outputStream = (CoyoteOutputStream) response.getOutputStream();
    Class<CoyoteOutputStream> coyoteOutputStreamClass = CoyoteOutputStream.class;
    Field obField = coyoteOutputStreamClass.getDeclaredField("ob");
    if (obField.getType().toString().endsWith("OutputBuffer")) {
        obField.setAccessible(true);
        org.apache.catalina.connector.OutputBuffer outputBuffer = (org.apache.catalina.connector.OutputBuffer) obField.get(outputStream);
        Class<org.apache.catalina.connector.OutputBuffer> opb = org.apache.catalina.connector.OutputBuffer.class;
        Field outputChunkField = opb.getDeclaredField("outputChunk");
        outputChunkField.setAccessible(true);
        if (outputChunkField.getType().toString().endsWith("ByteChunk")) {
            ByteChunk bc = (ByteChunk) outputChunkField.get(outputBuffer);
            Integer length = bc.getLength();
            if (length == 0) return null;
            responseContent = new String(bc.getBytes(), "UTF-8");
            Integer responseLength = StringUtils.isBlank(responseContent) ? 0 : responseContent.length();
            if (responseLength < length) {
                responseContent = responseContent.substring(0, responseLength);
            } else {
                responseContent = responseContent.substring(0, length);
            }

        }
    }
    return responseContent;
}
/**
*获取响应返回json内容
*
*@param响应
*@返回
*@抛出异常
*@NoSuchFieldException
*@galacessException
*/
公共字符串getResponseContent(HttpServletResponse响应)引发IOException、NoSuchFieldException、IllegaAccessException{
字符串responseContent=null;
CoyoteOutputStream outputStream=(CoyoteOutputStream)响应。getOutputStream();
Class coyoteOutputStreamClass=CoyoteOutputStream.Class;
字段obField=coyoteOutputStreamClass.getDeclaredField(“ob”);
if(obField.getType().toString().endsWith(“OutputBuffer”)){
obField.setAccessible(true);
org.apache.catalina.connector.OutputBuffer OutputBuffer=(org.apache.catalina.connector.OutputBuffer)obField.get(outputStream);
类opb=org.apache.catalina.connector.OutputBuffer.Class;
Field outputChunkField=opb.getDeclaredField(“outputChunk”);
outputChunkField.setAccessible(true);
if(outputChunkField.getType().toString().endsWith(“ByteChunk”)){
ByteChunk bc=(ByteChunk)outputChunkField.get(outputBuffer);
整数长度=bc.getLength();
if(length==0)返回null;
responseContent=新字符串(bc.getBytes(),“UTF-8”);
整数responseLength=StringUtils.isBlank(responseContent)?0:responseContent.length();
if(响应长度<长度){
responseContent=responseContent.子字符串(0,responseLength);
}否则{
responseContent=responseContent.substring(0,长度);
}
}
}
返回响应内容;
}
当响应json短时,代码运行良好。但是当返回json太长时,responseContent只包含部分响应内容,在日志记录之前解析内容失败(需要解析json并获取一些写入数据库的值)


如何调整响应并获得完整的响应内容?

增加tomcat defult缓冲区大小:

//default buffer size is:8*1024,in OutputBuffer class
//public static final int DEFAULT_BUFFER_SIZE = 8*1024;         
response.setBufferSize(2048 * 20);

这不是一个完美的解决方案,当响应大小超过2048*20时,它将遇到异常。但可以处理大多数响应。

您是否计算了响应大小并使用tomcat响应限制进行了检查