背景html
昨天同事遇到了error一块儿看了一下感受比较重要在这记录一下java
基本状况是页面上选中9K+的数据向后台发送请求,而后系统就崩了。。。spring
error信息以下apache
More than the maximum number of request parameters (GET plus POST) for a single request ([10,000]) were detected. api
Any parameters beyond this limit have been ignored. To change this limit, set the maxParameterCount attribute on the Connector.tomcat
简说 单次请求的参数超出限制,经过maxParameterCount来更改容器的限制。app
经验里对于tomcat容器的设定最对就是端口号,超时,最大线程的设置比较多 less
这个【maxParameterCount 】的设定尚未过而后到网上去翻了翻,函数
在官网的文档(tomcat doc)里找到了以下spring-boot
maxParameterCount |
The maximum number of parameter and value pairs (GET plus POST) which will be automatically parsed by the container. Parameter and value pairs beyond this limit will be ignored. A value of less than 0 means no limit. If not specified, a default of 10000 is used. Note that FailedRequestFilter filter can be used to reject requests that hit the limit. |
|
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 zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit. |
简说
maxParameterCount 是
tomcat容器来限定你 单次请求的参数的最大数量,默认是10000。因此经过这个属性你能够根据状况给设定适当的值。固然也有超出1w的状况怎么办?
上面文档里也有给出答案 小于0的设定能够禁用此制限。这也是不少网上资料设置-1的缘由。
maxPostSize 是http-post单次请求内容或者说数据的最大限制,默认值为2M。一样小于0的设定能够禁用此制限
具体使用
tomcat/conf/server.xml文件中找到以下节点
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
(这里有好几个connector看本身用的是哪一个端口的)
修改后
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="-1" maxParameterCount="-1"/>
这是一般的作法,但有时若是tomcat容器内置的话你可能都找不到server.xml文件,
好比spring boo项目内置tomcat
这时候一般会想到 能够配置在application.properties里
而后翻一下看看application.properties里怎么配置
从spring-boot的doc看到只看到以下
server.tomcat.max-http-post-size=0 # Maximum size in bytes of the HTTP post content. |
并无找到关于maxParameterCount信息,猜想不支持在配置文件里配置
继续翻
找到能够写在java类里的方法
@Bean public EmbeddedServletContainerFactory mbeddedServletContainerFactory() { TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory = new TomcatEmbeddedServletContainerFactory(); tomcatEmbeddedServletContainerFactory.addConnectorCustomizers(connector ->{ connector.setMaxPostSize(2); System.out.println("connector.getMaxPostSize: "+connector.getMaxPostSize()); System.out.println("connector.getPort: "+connector.getPort()); }); return tomcatEmbeddedServletContainerFactory; }
代码自己并很少,过程比较曲折,大概说一下
EmbeddedServletContainerCustomizer
这是一个自定义内置容器的接口,经过实现它能够建立内置容器
而后还找到已经实现了它类,
AbstractEmbeddedServletContainerFactory,
JettyEmbeddedServletContainerFactory,
TomcatEmbeddedServletContainerFactory,
UndertowEmbeddedServletContainerFactory
这里看到了定制tomcat容器的工厂类
继续看看这个类里都什么可用(源码有点多就不贴了贴一张截图)
这里就是咱们此次用到的函数了为何标记两个由于上边那个也可用,下面是官网给的说明
固然其它的属性也能够在这里设定贴一下官网链接就再也不代码体现了。