I want to create a WebSocket based on an example. The only spin is that I'm running my application in embedded 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!");
}
}
Configuration(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 mapping is at /websocket/*
From the log It seems like the spring-websocket is initialized successfully however I'm getting this error, when I try to connect to the 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
Dependencies are: tomcat-embed-core, tomcat-embed-websocket, tomcat-juli and because of (Spring websocket example - error - Are you running in a Servlet container that supports J SR-356?) javax.websocket-api and tomcat-websocket
What am I missing?
Thanks in advance!