javaspringspring-boottomcat10embedded-tomcat

how to set maxHttpHeaderSize in spring-boot 3.x


As stated in the Spring Boot 3 Migration Guide the server.max-http-header-size property has been deprecated. You can use the server.max-http-request-header-size property to set the max http request header size only.

I get the following Exception:

org.apache.coyote.http11.HeadersTooLargeException: An attempt was made to write 
more data to the response headers than there was room available in the buffer. 
Increase maxHttpHeaderSize on the connector 
or write less data into the response headers.

I need to increase the max http response header size in an embedded Tomcat 10. Tomcat 10 supports both the maxHttpHeaderSize and the maxHttpResponseHeaderSize attributes.

How I can set these in spring boot 3.x with a WebServerFactoryCustomizer?


Solution

  • You can create a WebServerFactoryCustomer as shown in the Spring Boot documentation.

    For your use case, that would look something like this:

        @Component
        public class TomcatCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
    
            @Override
            public void customize(TomcatServletWebServerFactory server) {
                server.addConnectorCustomizers((connector) -> {
                    ProtocolHandler handler = connector.getProtocolHandler();
                    if (handler instanceof AbstractHttp11Protocol<?> protocol) {
                        protocol.setMaxHttpResponseHeaderSize(x);
                    }
                });
            }
    
        }
    

    Spring Boot 3.1.0 will introduce a property server.tomcat.maxHttpResponseHeaderSize to make this easier to configure.