You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3.0.0中'Request header is too large'错误的解决方法

Spring Boot 3.x 解决「Request header is too large」问题

在Spring Boot 3.x中,server.max-http-header-size确实已被弃用,官方将其拆分为请求和响应头大小两个独立配置,以下是两种合规的解决方式:

1. 配置文件方式(最简方案)

直接在application.properties或application.yaml中使用新的配置项,专门针对请求头设置上限:

application.properties

# 设置最大请求头大小,支持KB/MB单位,示例为16KB
server.max-http-request-header-size=16KB

application.yaml

server:
  max-http-request-header-size: 16KB

如果需要同时调整响应头大小,可以额外添加server.max-http-response-header-size配置。

2. 编程式配置(灵活定制)

如果需要更细粒度控制Tomcat的Connector参数,比如针对不同环境设置不同值,或者结合其他Tomcat配置,可以通过配置类实现:

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TomcatHeaderConfig {

    @Bean
    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> tomcatHeaderCustomizer() {
        return factory -> {
            factory.addConnectorCustomizers(connector -> {
                // 设置全局最大请求头大小(字节),此处设为16KB
                connector.setMaxHttpHeaderSize(16 * 1024);
                
                // Tomcat 10.1+支持单独设置请求/响应头大小,按需启用
                // connector.setAttribute("maxHttpRequestHeaderSize", 16 * 1024);
                // connector.setAttribute("maxHttpResponseHeaderSize", 8 * 1024);
            });
        };
    }
}

这段代码通过自定义Tomcat工厂,直接修改Connector的参数,适配你的Tomcat 10.1.1版本,完全符合Spring Boot 3.x的规范。

内容的提问来源于stack exchange,提问作者Shuvradeb Saha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 09:45:27