More than the maximum number of request parameters (GET plus POST) ..([10,000])..MultipartException.
今天遇到了一个老接口,这个接口上传文件的同时还绑定了很多各种参数,
请求时直接报错
或者
顺着报错排查,在 package org.springframework.boot.web.embedded.tomcat;这个包下面找到了TomcatServletWebServerFactory这个工厂类
里面有这样一个集合
private final List<Connector> additionalTomcatConnectors = new ArrayList<>();
最终定位到了Connector类的这里
默认值是10000,显然这个接口超过了默认值(不想吐槽),为啥不直接传一个json字符串呢?
所以需要重新设置参数
package study.demo.web.config;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
private final Environment environment;
public TomcatCustomizer(Environment environment) {
this.environment = environment;
}
@Override
public void customize(TomcatServletWebServerFactory factory) {
int maxParameterCount = environment.getProperty("server.tomcat.max-parameter-count", int.class, -1);
factory.addConnectorCustomizers(connector -> connector.setMaxParameterCount(maxParameterCount));
}
}
server:
tomcat:
# 不限制
max-parameter-count: -1
spring:
servlet:
multipart:
max-file-size: 512MB
max-request-size: 512MB