java-嵌入式tomcat 8.0.21中的Spring Websocket
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-嵌入式tomcat 8.0.21中的Spring Websocket,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2729字,纯文字阅读大概需要4分钟。
内容图文
我想基于一个示例创建一个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
我想念什么?
提前致谢!
解决方法:
也许我找到了解决您问题的方法.
您需要对嵌入式tomcat上下文使用WsSci servlet容器初始化程序:
context.addServletContainerInitializer(new WsSci(),null);
https://github.com/spring-projects/spring-boot/issues/439
内容总结
以上是互联网集市为您收集整理的java-嵌入式tomcat 8.0.21中的Spring Websocket全部内容,希望文章能够帮你解决java-嵌入式tomcat 8.0.21中的Spring Websocket所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。