如何在Spring Boot中覆盖嵌入式Tomcat的websocket.textBufferSize属性?
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

