找不到Spring Boot Websocket 404

时间:2018-12-06 09:14:04

标签: spring spring-boot spring-ws spring-websocket

我正在运行spring boot,尝试连接到ws端点时在客户端出现404错误。我在想它的终点网址,但不确定。.

客户端代码

let wsUri = "ws://localhost:8080/chat"
let websocket = new WebSocket(wsUri);

spring config

package coffee.web;

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

    @EnableWebSocket
    public class WebSocketConfig implements WebSocketConfigurer {

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

      @Bean
      public ChatServer chatServer() {
        return new ChatServer();
      }

    }

2 个答案:

答案 0 :(得分:0)

由于该请求由调度程序Servlet像正常的HTTP请求一样处理。所以需要在WebSocketConfig类中添加@Controller注释

@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer

答案 1 :(得分:0)

我遇到404错误,然后添加了控制器,然后随着您的获取,我得到了403,403就像访问限制一样,所以我为该端点删除了CSRF过滤器,然后它开始工作。希望对您有帮助