Spring MVC和WebSockets

时间:2015-05-25 11:06:46

标签: spring-mvc websocket spring-4

我尝试将一个WebSocket端点添加到我的Spring 4 Web应用程序中。

我遵循tutorial

我创建了Handler和一个Interceptor,我注册了这些配置,在mvc-dispatcher-servlet.xml文件中添加了这个配置。 DispatcherServlet

<bean id="websocket" class="co.syntx.example.websocket.handler.WebsocketEndPoint"/>

<websocket:handlers>
    <websocket:mapping path="/websocket" handler="websocket"/>
    <websocket:handshake-interceptors>
    <bean class="co.syntx.example.websocket.HandshakeInterceptor"/>
    </websocket:handshake-interceptors>
</websocket:handlers>

我的问题是处理程序未注册,当我尝试连接时出现404错误

ws:localhost:8080/<app-context>/websocket

我也尝试使用&#39; xml-less&#39;配置(如下),但我尝试在registerWebSocketHandlers中添加断点,但从不调用该方法。

import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myHandler(), "/myHandler");
    }

    @Bean
    public WebSocketHandler myHandler() {
        return new MyHandler();
    }

}

0 个答案:

没有答案