这是我在春天设置一个简单的websocket控制器:
@Controller
@RequestMapping("/myHandler")
public class MyHandler extends TextWebSocketHandler {
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String reply = "Hello there!!!!!!!!";
session.sendMessage(new TextMessage(reply));
}
}
MCV-调度-servlet.xml中
<websocket:handlers allowed-origins="http://localhost">
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
</websocket:handshake-interceptors>
</websocket:handlers>
<bean id="myHandler" class="com.aexp.socket.MyHandler"/>
我已将所有jar文件更新为正确版本 但是我得到了以下错误:
HTTP Status 500 - Request processing failed; nested exception is org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/Websocket/myHandler; nested exception is java.lang.NoSuchMethodError: org.springframework.web.util.WebUtils.isValidOrigin(Lorg/springframework/http/HttpRequest;Ljava/util/Collection;)Z
root cause
org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/Websocket/myHandler; nested exception is java.lang.NoSuchMethodError: org.springframework.web.util.WebUtils.isValidOrigin(Lorg/springframework/http/HttpRequest;Ljava/util/Collection;)Z
org.springframework.web.socket.server.support.WebSocketHttpRequestHandler.handleRequest(WebSocketHttpRequestHandler.java:135)
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.java:49)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
当我尝试在JS中创建WebSocket对象时,我得到以下内容:
(Opcode -1)
这是我第一次尝试使用WebSockets,因此不确定我缺少什么。有人能指出我正确的方向。感谢。
答案 0 :(得分:0)
尝试按如下方式修改.xml配置:
<websocket:handlers allowed-origins="http://localhost">
<websocket:mapping path="/myHandler" handler="myHandler"/>
<websocket:handshake-interceptors>
<bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor"/>
<websocket:sockjs/>
</websocket:handshake-interceptors>
希望有所帮助!
答案 1 :(得分:0)
你错过了“spring-web”依赖。 将以下内容添加到您的pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
此外,您可能需要运行“干净”目标,并重建项目。