Spring Boot WebSocket(STOMP)连接localhost:8080/ws失败求助
Hey there! Let's dig into why your WebSocket connection keeps failing with that "Whoops! Lost connection to http://localhost:8080/ws" error. Based on your scenario and the code snippet you shared, here are the most likely issues and fixes to try:
Check your endpoint registration in the WebSocket config
It looks like you might be missing the proper endpoint setup in yourWebSocketConfigurationclass. Make sure you've overridden theregisterStompEndpointsmethod to map your/wspath and enable SockJS fallback. Here's what that should look like:@Override public void registerStompEndpoints(StompEndpointRegistry registry) { // Register the /ws endpoint and enable SockJS for browsers that don't support native WebSockets registry.addEndpoint("/ws").withSockJS(); // If your frontend is on a different origin (port/domain), add CORS allowed origins: // registry.addEndpoint("/ws").setAllowedOrigins("http://localhost:3000").withSockJS(); }Double-check that your frontend is connecting to the exact same path (e.g.,
new SockJS('/ws')in your client code).Verify your message broker configuration
A misconfigured message broker can cause connections to drop right after handshake. Ensure you've overriddenconfigureMessageBrokerto set up the correct prefixes:@Override public void configureMessageBroker(MessageBrokerRegistry config) { // Prefix for messages sent from frontend to your backend controllers config.setApplicationDestinationPrefixes("/app"); // Enable an in-memory broker for topics/queues (adjust prefixes to match your use case) config.enableSimpleBroker("/topic", "/queue"); }Without this, the broker won't route messages properly, leading to unexpected disconnections.
Check your dependencies and Spring version
Make sure you have the correct WebSocket starter dependency in your project. For Spring Boot, that's:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency>Version mismatches between Spring modules can also cause connection issues—ensure all your Spring dependencies are using a consistent version.
Inspect detailed error logs
The "Lost connection" message is pretty generic. Check your browser's developer tools (Network tab, look for the WebSocket request) and your backend logs for more details:- Is the initial handshake returning a 404? That means the
/wsendpoint isn't registered correctly. - Are there CORS errors? You'll see those in the browser console, which means you need to add allowed origins to your endpoint.
- Does the backend log show any exceptions during connection setup? That could point to a misconfiguration in your message broker or handler beans.
- Is the initial handshake returning a 404? That means the
Ensure no firewall/proxy is blocking the connection
If you're running behind a proxy or local firewall, it might be interrupting the WebSocket connection. Try testing on a clean environment without proxies, or ensure your proxy is configured to support WebSocket (which uses theUpgradeheader).
内容的提问来源于stack exchange,提问作者appzone_oto

