Pbb
Pbb

Reputation: 428

spring boot websocket api 404 not found

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();
   }
}

Upvotes: 0

Views: 3487

Answers (3)

bmarcu
bmarcu

Reputation: 13

I've just managed to solve this error message myself, so I'm posting my solution, hopefully, it will save somebody some time.

In my case it was the project structure, and the way the code was divided between packages.

By default, Spring scans the package where the file annotated with @SpringBootApplication is located and its subpackages. If your Web Socket Configuration and Web Socket Handler are placed somewhere else, which was my case, then Spring won't find them by default. To solve this, use @ComponentScan(basePackages = {...}) annotation in the @SpringBootApplication file.

Upvotes: 0

Mallik Rajini
Mallik Rajini

Reputation: 31

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

Upvotes: 2

Alien
Alien

Reputation: 15878

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

Upvotes: 3

Related Questions