客户端发送一个http请求,请求方式是post,现在我不想用("")这种方式取数据,而想从输入流servletInputStream中得到所有的请求体字符串,再自行转换(各位读者别管这种方式在生产环境中会不会用,在这里只是用于学习讨论)。
客户端发送的http请求数据如下:
POST /webInterface/testPost HTTP/1.1
Host: localhost:8099
Cache-Control: no-cache
Postman-Token: aa4d51bc-c29e-ff4e-a14d-0f8c60e0dbfd
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="tradeInfos"
<?xml version="1.0" encoding="UTF-8"?> <tradeinfolist> <tradeinfo> <accountid>185839620012015</accountid> <tradenum></tradenum> </tradeinfo> <tradeinfo> <accountid>186812771092016</accountid> <tradenum>xxx</tradenum> </tradeinfo> </tradeinfolist>
------WebKitFormBoundary7MA4YWxkTrZu0gW--
服务器端接受代码如下:
@RequestMapping("/tradeNumNotify")
public String tradeNumNotify(HttpServletRequest servletRequest)
{
。。。。。
String tradeInfos = "";
try {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( () ));
String nextLine = ();
while( nextLine != null )
{
tradeInfos += nextLine;
nextLine = ();
}
} catch (IOException e) {
();
}
。。。。
}
实际情况是:
inputStream中的数据为空。所以nextLine也为空。
原因:
springmvc会在进入函数之前对request中的inputstream中的内容进行检查,如果发现是一个键值对对象,就讲它放入request中的Parameter数组中,这就会造成我们进入方法之前springmvc已经去调用了inputstream的读方法了,其中的内容已经被消耗掉了,所以我们在方法中无法再通过inputStream或得到请求体的内容。
解决办法:
目前没有找到解决办法,网上有说通过@RequestBody进行获取,但是我测试过之后发现不行,请求体内的数据如果键值对的 ,无论用不用@RequestBody, springmvc都会去读取inputStream的内容,故还是不行,先打个标记在这里,以后找到办法了再回来看