spring - SockJS 无法创建/连接到 Spring WebSocket

标签 spring spring-mvc stomp spring-websocket sockjs

我正在学习 Spring WebSocket。我已成功运行此 Spring WebSocket教程。现在我试图将它按原样合并到我现有的 Spring MVC 应用程序中。当我从 Chrome 浏览器运行它时,我在其开发控制台中看到以下错误。

Chrome 控制台

Opening Web Socket...
GET http://localhost:8080/MyAppName/api/gs-guide-websocket/info?t=1497735312528 500 (Internal Server Error) -- abstract-xhr.js:132
Whoops! Lost connection to http://localhost:8080/MyAppName/api/gs-guide-websocket -- stomp.min.js:8 

服务器端错误
javax.servlet.ServletException: Could not resolve view with name '/MyAppName/api/gs-guide-websocket/info' in servlet with name 'MyAppName'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1262)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)

客户端
function connect() {
    var socket = new SockJS('/MyAppName/api/gs-guide-websocket');
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function (frame) {
        setConnected(true);
        console.log('Connected: ' + frame);
        stompClient.subscribe('/topic/greetings', function (greeting) {
            showGreeting(JSON.parse(greeting.body).content);
        });
    });
}

服务器端
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/MyAppName/api/gs-guide-websocket").withSockJS();
    }

}

我已经尝试解决这个问题几个小时了。我该如何解决?

最佳答案

如果您的应用程序上下文是 MyAppName那么你不需要在 addEndpoint 中指定它方法 - 此路径与您的应用程序上下文相关。

大概 registry.addEndpoint("/MyAppName/api/gs-guide-websocket")使用路径 /MyAppName/MyAppName/api/gs-guide-websocket 注册端点

关于spring - SockJS 无法创建/连接到 Spring WebSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44609914/

相关文章:

angular - 有没有办法在 websocket 连接中捕获关闭事件?

java - Spring Websocket ChannelInterceptor 未触发 CONNECT 事件

java - Spring 测试 : Database is initialized twice in test

java - 创建 Spring MVC 应用程序

java - 给定 View 类 [org.springframework.web.servlet.view.tiles3.TilesView] 不是 [org.springframework.web.servlet.view.AbstractUrlBasedView] 类型

java - 将 View 解析器添加到不同的请求

spring-mvc - Spring中更新多个数据库行-Mybatis

java - 如何使用 Spring WebSocket 向 STOMP 客户端发送错误消息?

spring - 如何在 Spring @Configuration 类中使用 HikariCP 配置数据源?

java - NoClassDefFound错误: org/springframework/context/support/ClassPathXmlApplicationContext