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

如何在Spring Boot中覆盖嵌入式Tomcat的websocket.textBufferSize属性?

How to set org.apache.tomcat.websocket.textBufferSize in Spring Boot with embedded Tomcat

I get it, since Spring Boot doesn’t expose a dedicated out-of-the-box property for this Tomcat-specific WebSocket parameter, you’ll need to customize the embedded Tomcat directly. Here are two reliable approaches to adjust the text message buffer size:

1. Add Servlet Context Initialization Parameter

You can register a ServletContextInitializer bean to inject the parameter into Tomcat’s servlet context during startup:

import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

@Configuration
public class TomcatWebSocketConfig {

    @Bean
    public ServletContextInitializer servletContextInitializer() {
        return servletContext -> {
            // Replace 16384 with your desired buffer size in bytes
            servletContext.setInitParameter("org.apache.tomcat.websocket.textBufferSize", "16384");
        };
    }
}

Tomcat’s WebSocket module will automatically pick up this initialization parameter and use it instead of the default 8192-byte value.

2. Customize the Tomcat WebSocket Container Directly

If you’re using Spring’s WebSocket abstractions (like @ServerEndpoint or @EnableWebSocket), this approach is more integrated with Spring Boot’s customization patterns:

import org.springframework.boot.web.embedded.tomcat.TomcatWebSocketContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebSocketContainerConfig {

    @Bean
    public TomcatWebSocketContainerCustomizer tomcatWebSocketContainerCustomizer() {
        return container -> {
            // Set your preferred buffer size here
            container.setMaxTextMessageBufferSize(16384);
        };
    }
}

The setMaxTextMessageBufferSize method maps directly to the org.apache.tomcat.websocket.textBufferSize parameter, so this achieves the exact same result as the first method.

Either of these solutions will let you override the default buffer size to meet your application’s needs.

内容的提问来源于stack exchange,提问作者Brian P. Bailey

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:11:44