The Spring Boot Configure maxSwallowSize Tomcat tutorial shows you to configure maxSwallowSize in embedded Tomcat. Actually, you can’t configure the embedded Tomcat maxSwallowSize via the common application properties in Spring Boot. As i know this properties be used to specify the init parameters of the servlet context. However, maxSwallowSize is a property of the connector. That’s a different but we can always configure it via our own TomcatEmbeddedServletContainerFactory.
Other interesting posts you may like
Solution
We need to declare a TomcatEmbeddedServletContainerFactory bean and configure the maxSwallowSize like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import org.apache.coyote.http11.AbstractHttp11Protocol; import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; private int maxUploadSizeInMb = 10 * 1024 * 1024; // 10 MB @Bean public TomcatEmbeddedServletContainerFactory containerFactory() { return new TomcatEmbeddedServletContainerFactory() { protected void customizeConnector(Connector connector) { super.customizeConnector(connector); if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol) { // if maxUploadSizeInMb = -1, accept unlimited bytes (AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(maxUploadSizeInMb); } } }; } |
Note
maxSwallowSize: The maximum number of request body bytes (excluding transfer encoding overhead) that will be swallowed by Tomcat for an aborted upload. An aborted upload is when Tomcat knows that the request body is going to be ignored but the client still sends it. If Tomcat does not swallow the body the client is unlikely to see the response. If not specified the default of 2 megabytes will be used. A value of less than zero indicates that no limit should be enforced.
That’s all on the Spring Boot Configure maxSwallowSize Tomcat tutorial.
References
common application properties
The HTTP Connector