Spring MVC Restful Put方法无法获取参数值

时间:2022-01-05 20:07:33

Spring MVC Restful 无法通过@ReqeustParam获取参数值

原因是Tomcat只支持POST/GET获取参数值,对于PUT这些方法需要通过HttpPutFormContentFilter对其进行拦截过滤,该filter内部通过HttpServletRequestWrapper将参数塞进request的param中。

  • 解决该问题只需要在项目web.xml中加入代码:

<filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
  • 如果还是无法得到,可能是没开启spring的HiddenHttpMethod

加入如下代码:


<filter>
<filter-name>HttpPutFormContentFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpPutFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>