嵌入式tomcat 8.0.21中的Spring websocket
Posted
技术标签:
【中文标题】嵌入式tomcat 8.0.21中的Spring websocket【英文标题】:Spring websocket in embedded tomcat 8.0.21 【发布时间】:2016-08-29 13:26:22 【问题描述】:我想基于一个示例创建一个 WebSocket。唯一的变化是我在嵌入式 tomcat 中运行我的应用程序。
package com.test.websocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
public class WebSocketTest extends AbstractWebSocketHandler
private static Logger logger = LoggerFactory.getLogger(WebSocketTest.class);
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception
logger.debug("Recieved websocket message: " + message);
session.sendMessage(new TextMessage("thanks"));
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception
logger.info("WebSocket connection established!");
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception
logger.info("WebSocket connection closed!");
配置(websocket.spring.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket.xsd">
<websocket:handlers>
<websocket:mapping path="/socket" handler="webSocketHandler"/>
</websocket:handlers>
<bean id="webSocketHandler" class="com.test.websocket.WebSocketTest"/>
</beans>
Servlet 映射在 /websocket/*
从日志看来,spring-websocket 似乎已成功初始化,但是当我尝试连接到 websocket 时出现此错误。
Servlet.service() for servlet [spring-websocket] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:9876/websocket/socket; nested exception is java.lang.IllegalArgumentException: No 'javax.websocket.server.ServerContainer' ServletContext attribute. Are you running in a Servlet container that supports JSR-356?] with root cause
依赖项是:tomcat-embed-core、tomcat-embed-websocket、tomcat-juli 以及由于 (Spring websocket example - error - Are you running in a Servlet container that supports J SR-356?) javax.websocket-api 和 tomcat-websocket
我错过了什么?
提前致谢!
【问题讨论】:
【参考方案1】:也许我找到了解决您问题的方法。
您需要使用 WsSci servlet 容器初始化器来嵌入 tomcat 上下文:
context.addServletContainerInitializer(new WsSci(), null);
https://github.com/spring-projects/spring-boot/issues/439
【讨论】:
在没有 Spring Boot 的情况下似乎无法工作,例如在使用 Gradle Tomcat 插件时。以上是关于嵌入式tomcat 8.0.21中的Spring websocket的主要内容,如果未能解决你的问题,请参考以下文章
在 Spring Boot 中的多个连接器之间共享嵌入式 Tomcat 执行器
Spring Boot postgresql嵌入式tomcat启动失败