参考文章:
嵌入式Tomcat容器的参数(maxParameterCount&maxPostSize)设定,参数过多解决方案
周五同事说,请求的参数拿不到了。但是同一个接口请求参数太大就没有参数了,参数少的话服务端是有参数的。
打开浏览器的控制台,发现
POST
的请求参数中的有一个参数很大,所有的参数加起来有2.8M
了。网上查了一下Tomcat
的配置,
原来配置文件中有一个masPostSize
的参数。因此这个博客来看看tomcat
的maxParameterCount&maxPostSize
参数,看看是不是这个问题导致的。
这里截图看到线上是Content-Type: application/x-www-form-urlencoded;charset=UTF-8
的POST请求类型,Content-Length:
有问题的是2.8M,并不是这个截图所示的234B。
maxParameterCount
控制请求参数的个数,对于application/x-www-form-urlencoded or multipart/form-data
的POST请求来说是请求参数和请求体参数总个数。超出的参数获取不到maxPostSize
控制POST请求参数大小的限制。
application/x-www-form-urlencoded
大小超过的参数获取不到。 multipart/form-data
大小超过异常报错。tomcat的maxPostSize没有设置,默认的是2M,请求是application/x-www-form-urlencoded
类型的,所以也不会报错。参数字节数小的可以获取到,参数字节数大的就获取不到了。
https://tomcat.apache.org/
找到文档入口Reference
The maximum total number of request parameters (including uploaded files) obtained from the query string and, for POST requests, the request body if the content type is application/x-www-form-urlencoded or multipart/form-data. Request parameters 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 exceed 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 MiB). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="7"
maxParameterCount="2"
/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
Host>
Engine>
Service>
Server>
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxPostSize="7"
maxParameterCount="2"
/>
maxParameterCount
参数个数 ~/data/
~/data/ curl -s --location 'http://localhost:8080/?m=m&m1=m1&m2=m2' | jq .
{
"m": [
"m"
],
"m1": [
"m1"
]
}
~/data/
~/data/ curl -s --location 'http://localhost:8080/?m=m&m1=m1' | jq .
{
"m": [
"m"
],
"m1": [
"m1"
]
}
~/data/
~/data/
~/data/ curl -s --location --request POST 'http://localhost:8080/test?m=m&m1=m1&m2=m2' | jq .
{
"m": [
"m"
],
"m1": [
"m1"
]
}
~/data/
~/data/
~/data/ curl -s --location --request POST 'http://localhost:8080/test?m=m&m1=m1' | jq .
{
"m": [
"m"
],
"m1": [
"m1"
]
}
~/data/
~/data/ curl --location 'http://localhost:8080/test?m=m&m1=m1&m2=m2' \
--header 'Content-Type: application/x-www-form-urlencoded' -s \
--data-urlencode 'm3=m3' | jq .
{
"m": [
"m"
],
"m1": [
"m1"
]
}
~/data/
~/data/ curl --location 'http://localhost:8080/test?m=m' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'm3=m3' -s | jq .
{
"m": [
"m"
],
"m3": [
"m3"
]
}
~/data/
~/data/
maxPostSize
POST请求参数大小Content-Type: application/x-www-form-urlencoded
大小没有超过都可以获取到,超过大小都获取不到 ~/data/
~/data/ curl --location 'http://localhost:8080/test' -s \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'j=12345' | jq .
{
"j": [
"12345"
]
}
~/data/
~/data/
~/data/
~/data/ curl --location 'http://localhost:8080/test' -s \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'j=123456' | jq .
{}
~/data/
~/data/ curl --location 'http://localhost:8080/test' -s \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'j=1' \
--data-urlencode 'i=2' | jq .
{
"j": [
"1"
],
"i": [
"2"
]
}
~/data/
~/data/
~/data/ curl --location 'http://localhost:8080/test' -s \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'j=1' \
--data-urlencode 'i=23' | jq .
{}
~/data/
~/data/
multipart/form-data; boundary=
大小没有超过都可以获取到,超过大小报错 ~/data/
~/data/ curl --location 'http://localhost:8080/test' \
--form 'j="1234"' -s | jq .
{
"j": [
"1234"
]
}
~/data/ curl --location 'http://localhost:8080/test' -s \
--form 'j="12345"'|jq .
{
"timestamp": 1705816422926,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.web.multipart.MultipartException",
"message": "Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: The multi-part request contained parameter data (excluding uploaded files) that exceeded the limit for maxPostSize set on the associated connector",
"path": "/test"
}
~/data/
~/data/ docker pull tomcat:8.5.98
~/data/ docker run -d -p 8080:8080 -v /Users/admin/data/tomcat/webapps:/usr/local/tomcat/webapps tomcat:8.5.98
90f2cfa859c67e3886f67d8b862005c196944cbc037efc64e2e1417b450ae174
~/data/ server.xml的配置见上文
~/data/ docker cp ./server.xml 90f2cfa859c6:/usr/local/tomcat/conf/server.xml
~/data/ java 代码:https://github.com/xiaolixi/spring/tree/main/springboot-resttemplate
https://tomcat.apache.org/tomcat-8.5-doc/servletapi/javax/servlet/ServletRequest.html#getParameterMap()