0

In my spring boot app, I'm getting 404 error on the client side when I try to connect to the ws endpoint.

client side code

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();
   }
}
Alexpandiyan Chokkan
  • 1,025
  • 1
  • 10
  • 30
Pbb
  • 408
  • 6
  • 21

2 Answers2

2

Since the request is handled by dispatcher servlet as normal http request. so you need to add @Controller annotation to the WebSocketConfig class

@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer
Alien
  • 15,141
  • 6
  • 37
  • 57
1

i was getting 404 error then i have added the controller and then i was getting 403 as you are getting, 403 is like access restriction , so i have removed CSRF filter for that end point and then it is working. i hope it helps