1. 程式人生 > >Spring MVC POST 引數無法繫結

Spring MVC POST 引數無法繫結

Spring MVC 控制器,方法的引數突然繫結不上。

查看了相關的資料:這裡這裡,都是說與 Content-Type 屬性有關。
但從程式碼看,一切正常,並沒有網上描述的情況。
換了一臺電腦後,發現一切又恢復正常。想到最近優化過 Tomcat 。於是
最終確定問題的原因是 Tomcat 優化引數

<Connector port="8080" 
           maxPostSize="0"
           ....
/>

其中問題是在 maxPostSize="0"上。
當去掉時,一切正常。

maxPostSize : The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

大致意思是:maxPostSize 表示 post 引數的最大限制。如果設定為 0 或者 <0 則表示沒有限制。

但是為什麼設定為 0 時候,就沒有辦法傳遞引數了呢?

The docs aren’t quite right. maxPostSize=0 results in a limit of zero. -1 is the correct value for no limit. I’ll get that fixed shortly.

大致意思:maxPostSize=0 導致 post 可傳遞的大小限制為 0 。正確表示沒有限制應該設定為 -1 。

至此,應該是某些版本的 Tomcat 有此 Bug。至少確認 tomcat 8.5.11 有此問題。
maxPostSize 如果想要設定,可以設定為 -1。避免出現問題。