齊魯華夏 發表於 2026-1-13 10:05:43

springboot2.7报错:request header is too large问题及解决

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、报错内容</li><li>二、原因分析与解决方案概述</li><li>三、解决方法</li><ul class="second_class_ul"><li>3.1、调整应用程序配置(主要解决方案)</li><ul class="third_class_ul"><li>Spring Boot 2.x 配置</li><li>Spring Boot 3.x 配置</li></ul><li>3.2、通过代码配置(备用方案)</li><ul class="third_class_ul"></ul><li>3.3、直接配置 Tomcat(适用于外部 Tomcat)</li><ul class="third_class_ul"></ul></ul><li>四、注意事项</li><ul class="second_class_ul"></ul><li>五、总结</li><ul class="second_class_ul"></ul></ul></div><p>springboot2.7中,后台接口使用@RequestParam进行传参,但是传入的参数内容过大,导致报错&nbsp;</p>
<blockquote><p>request header is too large</p></blockquote>
<p class="maodian"></p><h2>一、报错内容</h2>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202601/2026011309461260.png" /></p>
<p><strong>问题原因</strong>:</p>
<p>请求头超过了tomcat的限制值。post请求是没有参数大小限制,但是服务器有自己的默认大小</p>
<p>SpringBoot内嵌的Tomcat服务器默认限制是<code>8KB</code></p>
<p class="maodian"></p><h2>二、原因分析与解决方案概述</h2>
<table><thead><tr><th>方面</th><th>说明</th><th>建议或默认值</th></tr></thead><tbody><tr><td>错误原因</td><td>HTTP 请求头过大</td><td>通常由于Cookie、Authorization、自定义头过大引起</td></tr><tr><td>Spring Boot 2.x 配置参数</td><td>server.max-http-header-size</td><td>单位支持 B, KB, MB</td></tr><tr><td>Spring Boot 3.x 配置参数</td><td>server.max-http-request-header-size (3.x中max-http-header-size已被弃用)</td><td>单位支持 B, KB, MB</td></tr><tr><td>Tomcat 默认值</td><td>8KB (8192 bytes)</td><td></td></tr><tr><td>Undertow 默认值</td><td>1MB</td><td></td></tr><tr><td>Jetty 默认值</td><td>8KB</td><td></td></tr></tbody></table>
<p><strong>解决方案概述</strong>:</p>
<p>主要通过<strong>调整配置文件</strong>增大请求头大小限制,并<strong>优化请求头内容</strong>。</p>
<p class="maodian"></p><h2>三、解决方法</h2>
<p class="maodian"></p><h3>3.1、调整应用程序配置(主要解决方案)</h3>
<p>根据你的 Spring Boot 版本和使用的配置文件格式(properties 或 yml),进行如下配置:</p>
<p class="maodian"></p><h4>Spring Boot 2.x 配置</h4>
<p>在 <code>application.properties</code> 文件中添加:</p>
<div class="jb51code"><pre class="brush:java;"># 设置最大 HTTP 请求头大小为 100KB (推荐初始值)
server.max-http-header-size=100KB
</pre></div>
<p>或者使用字节单位:</p>
<div class="jb51code"><pre class="brush:java;"># 设置最大 HTTP 请求头大小为 102400 字节 (100KB)
server.max-http-header-size=102400
</pre></div>
<p>在 <code>application.yml</code> 文件中添加:</p>
<div class="jb51code"><pre class="brush:yaml;">server:
max-http-header-size: 100KB
</pre></div>
<p>或者:</p>
<div class="jb51code"><pre class="brush:yaml;">server:
max-http-header-size: 102400
</pre></div>
<p class="maodian"></p><h4>Spring Boot 3.x 配置</h4>
<p>从 Spring Boot 3.0 开始,<code>max-http-header-size</code> 参数已被弃用,建议使用 <code>max-http-request-header-size</code>:</p>
<div class="jb51code"><pre class="brush:yaml;">server:
    max-http-request-header-size: 12KB
</pre></div>
<p class="maodian"></p><h3>3.2、通过代码配置(备用方案)</h3>
<p>如果配置文件方式不生效,你可以在启动类或配置类中通过代码配置:</p>
<div class="jb51code"><pre class="brush:java;">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 ServerConfig {

    @Bean
    public WebServerFactoryCustomizer&lt;TomcatServletWebServerFactory&gt; containerCustomizer() {
      return factory -&gt; factory.addConnectorCustomizers(connector -&gt; {
            connector.setMaxHttpHeaderSize(102400); // 设置为100KB
      });
    }
}
</pre></div>
<p class="maodian"></p><h3>3.3、直接配置 Tomcat(适用于外部 Tomcat)</h3>
<p>如果你的 Spring Boot 应用部署在<strong>独立的 Tomcat 服务器</strong>上,需要修改 Tomcat 的 <code>server.xml</code> 文件:</p>
<div class="jb51code"><pre class="brush:yaml;">&lt;Connector
    port="8080"
    protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443"
    maxHttpHeaderSize="102400" /&gt;
</pre></div>
<p><strong>重启 Tomcat</strong> 使配置生效。</p>
<p class="maodian"></p><h2>四、注意事项</h2>
<ul><li><strong>不要盲目设置过大值</strong>:设置过大的请求头限制会增加内存消耗,在并发请求时可能造成内存溢出(OOM)。应根据实际业务需求设置合理值。</li><li><strong>检查反向代理配置</strong>:如果你的应用前面有 <strong>Nginx</strong> 或 <strong>Apache</strong> 等反向代理服务器,这些服务器也可能有类似的请求头大小限制(例如 Nginx 的 <code>large_client_header_buffers</code> 指令),需要一并调整。</li></ul>
<p><strong>优化请求头设计</strong>:</p>
<ul><li>避免在请求头中存储大量数据(如大型 Base64 编码的图片)。</li><li>考虑将大数据存储在请求体(Body)而非请求头中。</li><li>对于 GET 请求参数过长的情况,考虑改为 POST 请求,将参数放在请求体中。</li></ul>
<p class="maodian"></p><h2>五、总结</h2>
<p>以上为个人经验,希望能给大家一个参考,也希望大家多多支持琼殿技术社区。&nbsp;</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>SpringBoot从Service层获取request.getHeader()的几种方式</li><li>SpringBoot中@RequestBody的伪表单提交场景</li><li>Springboot接收文件报错Required&nbsp;request&nbsp;part‘file‘is&nbsp;not&nbsp;present问题分析及解决</li><li>解决Spring/SpringBoot&nbsp;@RequestParam注解无法读取application/json格式数据问题</li><li>SpringBoot解析JSON报错400&nbsp;Bad&nbsp;Request的解决方案</li><li>SpringBoot中使用异步线程导致Request请求头丢失问题的解决方法</li><li>springboot中@RequestParam和@PathVariable区别</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: springboot2.7报错:request header is too large问题及解决