最近在使用spring security框架,发现一个问题,就是我通过request.getInputStream()方法读取了请求体中的json数据后,就发现在controller控制器层中无法再次获取到参数,会出现报错
I/O error while reading input message; nested exception is java.io.IOException: Stream closed
{
"timestamp": "2019-07-07T03:19:44.674+0000",
"status": 400,
"error": "Bad Request",
"message": "I/O error while reading input message; nested exception is java.io.IOException: Stream closed",
"path": "/test1"
}
这是因为request body请求体中的数据已经读取过一次了,所以无法第二次读取。如果能在读取后,直接在将数据写入到request body中,那么我们在控制器层就可以再次读取。
方法如下:
自定义一个request包装器WebHttpServletRequestWrapper ,继承RequestReaderHttpServletRequestWrapper这个类
该类源码如下:
package com.company.request;
import javax.servlet.ReadListener;
import javax.servlet.Servl