Spring mvc 带HttpPutFormContentFilter的Spring MVC can';使用PUT方法时无法获取RequestBody

Spring mvc 带HttpPutFormContentFilter的Spring MVC can';使用PUT方法时无法获取RequestBody,spring-mvc,put,Spring Mvc,Put,我在web.xml中添加了HttpPutFormContentFilter 下面是一个操作,用于重新激活GET、POST、DELETE和PUT方法 @RequestMapping(value = "/**") public ResponseEntity<byte[]> proxy(HttpServletRequest request,@RequestParam MultiValueMap<String, String> params, @RequestBody byte[

我在
web.xml
中添加了
HttpPutFormContentFilter

下面是一个操作,用于重新激活
GET
POST
DELETE
PUT
方法

@RequestMapping(value = "/**")
public ResponseEntity<byte[]> proxy(HttpServletRequest request,@RequestParam MultiValueMap<String, String> params, @RequestBody byte[] body, @RequestHeader MultiValueMap<String, String> headers) {
@RequestMapping(value=“/**”)
公共响应属性代理(HttpServletRequest请求、@RequestParam多值映射参数、@RequestBody字节[]体、@RequestHeader多值映射头){
当我使用
POST
并添加
application/x-www-form-urlencoded
头时,我可以获得请求正文和请求参数

当我使用
PUT
并添加
application/x-www-form-urlencoded
头时,我可以获取请求参数,但无法获取请求正文

HttpPutFormContentFilter中有任何错误吗?

根据(参见第1.1和3.1.1章),当您收到POST请求且内容类型为
application/x-www-form-urlencoded
时,需要通过
HttpServletRequest\getParameterXXX()提供表单数据
方法。PUT请求不是这样

在所有情况下,
HttpServletRequest
的主体都可以作为
InputStream
Servlet
Filter
实例使用

对于POST,当Spring看到

@RequestParam MultiValueMap<String, String> params
@RequestBody byte[] body
它使用
RequestResponseBodyMethodProcessor
,该处理器使用
ByteArrayHttpMessageConverter
HttpServletRequest
输入流中读取数据,以填充
字节[]

一旦读取了
HttpServletRequest
InputStream
,就无法重新读取(在默认配置中)

对于PUT,因为Servlet容器不在PUT请求的
HttpServletRequest
中存储表单参数,Spring决定引入
HttpPutFormContentFilter
。此
Filter
读取
HttpServletRequest
主体,以在
HttpServletRequestWrapper
中填充自己的参数映射>它交给你的

完成此操作后,请求参数可用于
RequestParamMapMethodArgumentResolver
,但当
RequestResponseBodyMethodProcessor
尝试填充
字节[]
时,
输入流中没有剩余的字节,因此将其保留为空



一种解决方法是创建自己的
过滤器
(必须在
HttpPutFormContentFilter
之前执行,因此这是一种不好的做法),它将
HttpServletRequest
包装在
HttpServletRequestWrapper
中,它将
InputStream
缓冲在
ByteArrayInputStream
中,以便您可以根据需要多次重新读取它。

您收到错误了吗?还是一个空字节数组?@SotiriosDelimanolis我收到一个enpty字节数组。没有错误。除了太多了!在我的例子中,我不需要
@RequestParam
。所以我删除了
HttpPutFormContentFilter
,它会工作的。