POST、PUT、DELETE請求不指定Content-Type時,默認使用application/x-www-form-urlencoded
這時參數提交方式是form data
POST請求默認會將參數填充的servlet的請求參數中,PUT、DELETE不會。所以會有后兩種請求拿不到參數的問題。怎么解決?
兩個思路:
(1)將PUT、DELETE請求轉為POST
(2)用Filter將參數填充的servlet的請求參數中
下面是第二種思路:
舊的spring 版本配置 HttpPutFormContentFilter,只能解決PUT請求參數的填充。可以自定義補充實現DELETE的請求的邏輯,參考FormContentFilter(照抄也沒問題,親測)
新的spring 版本配置 FormContentFilter,可以解決PUT、DELETE請求參數的填充。
配置web.xml
<filter>
<filter-name>MyFormContentFilter</filter-name>
<filter-class>com.test.filters.MyFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFormContentFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
GET請求參數提交方式是query string parameters
@RequestParam就可以解析