You need to enable JavaScript to run this app. Spring Websocket页面可正常访问但连接失败报404问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Spring Websocket页面可正常访问但连接失败报404问题求助

项目相关文件

  • WebSocketConfig.java
  • ChatController.java
  • ChatMessage.java
  • MessageType.java
  • index.html
  • main.js

build.gradle配置

plugins {
    id 'com.github.johnrengelman.shadow' version '7.0.0'
    id 'org.springframework.boot' version '2.5.0'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'application'
}

def versionName

try {
    def result = new groovy.json.JsonSlurper().parseText(file('beta.json').text)
    versionName = result.bot.version
} catch (ignored) {
    versionName = 'v0.0.0'
}

sourceCompatibility = '15'
group 'com.beanbeanjuice'
mainClassName = 'com.beanbeanjuice.main.CafeBot'
version versionName

repositories {

    // Central Maven repository.
    mavenCentral()

    maven {
        url 'https://m2.dv8tion.net/releases'
    }

}

dependencies {

    // JUnit, for Unit Testing
    testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.8.0-M1'
    testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine'

    // Json Slurper
    implementation group: 'org.codehaus.groovy', name: 'groovy-json', version: '3.0.8'

    // Jetbrains Annotations
    implementation group: 'org.jetbrains', name: 'annotations', version: '21.0.1'

    // Discord JDA, for Discord things
    implementation group: 'net.dv8tion', name: 'JDA', version: '4.2.1_265'

    // Twitch Connections
    implementation group: 'com.github.twitch4j', name: 'twitch4j', version: '1.4.0'

    // Lava Player
    implementation group: 'com.sedmelluq', name: 'lavaplayer', version: '1.3.77'

    // Spotify API
    implementation group: 'se.michaelthelin.spotify', name: 'spotify-web-api-java', version: '6.5.4'

    // JSOUP for Parsing
    implementation group: 'org.jsoup', name: 'jsoup', version: '1.13.1'

    // JSON
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.12.3'
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.12.3'

    // Logback
    implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

    // SQL Connection
    implementation group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.7.3'

    // Time Stuff (For Birthdays)
    implementation group: 'joda-time', name: 'joda-time', version: '2.10.10'

    // Apple Music/Itunes API
    implementation group: 'be.ceau', name: 'itunes-api', version: '4.6.0'

    // Spring Stuff
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '2.4.3'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-reactor-netty', version: '2.4.3'
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.4.3'

}

test {
    useJUnitPlatform()
}

compileJava.options.encoding = 'UTF-8'
compileTestJava.options.encoding = 'UTF-8'

问题现象

通过localhost:8080访问可正常加载登录页面,触发WebSocket连接操作时,弹出main.js第50行定义的报错提示:Could not connect to the WebSocket server. Please refresh this page to try again!。

进一步排查发现WebSocket连接请求返回404错误,服务端日志显示请求路径为/console/info,该路径被映射到静态资源处理器后未找到对应资源,最终返回404状态码。

已确认的非影响报错

  • 日志中org.apache.tomcat.jni相关LibraryNotFoundError属于正常现象,不影响服务运行
  • 切换JDK 16后已解决java.net.UnixDomainSocketAddress相关ClassNotFoundException,但WebSocket连接问题仍未解决

解决方案

  • 端口一致性校验:从错误日志可见服务端实际监听端口为4002,需检查main.js中WebSocket连接地址的端口是否和服务端启动端口一致,若服务端配置了server.port=4002,前端连接地址需对应修改为ws://localhost:4002/[你的端点路径]。
  • 路径匹配校验:检查WebSocketConfig.java中注册的STOMP端点路径是否和前端连接路径匹配,若后端注册的端点为/ws,前端SockJS连接地址也需要对应设置为/ws,当前日志显示前端请求的是/console/info,路径不匹配是导致404的直接原因。
  • 依赖版本统一:当前build.gradle中Spring Boot主版本为2.5.0,但WebSocket相关starter依赖版本为2.4.3,版本不统一会导致功能异常,建议将所有Spring Boot starter依赖的版本统一为2.5.0,和主版本保持一致。
  • 配置注解校验:确认WebSocketConfig类上已经添加了@Configuration和@EnableWebSocketMessageBroker注解,否则Spring不会加载WebSocket相关配置,也会导致端点找不到的问题。
  • 上下文路径校验:如果Spring Boot项目配置了server.servlet.context-path参数,前端WebSocket连接路径需要添加上下文路径作为前缀,否则也会触发404错误。

内容的提问来源于stack exchange,提问作者beanbeanjuice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 07:54:10