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
,它会工作的。