【问题标题】:Spring websocket getting 404 not found找不到 Spring websocket 得到 404
【发布时间】:2015-03-16 01:23:18
【问题描述】:

我正在尝试使用 spring 创建一个 websocket 端点。但是每当我尝试从客户端连接时,我都会得到 404。我还有一个带有

的 Java 实现
@ServerEndpoint(value = "/websocket")

效果很好。下面是我的 spring 实现的代码,它不起作用。我在这里做错了什么?

package com.tlab.endpoint;

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

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

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

    @Bean
    public WebSocketHandler socketHandler() {
        return new SocketHandler();
    }

}

下面是处理程序

package com.tlab.endpoint;

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;


public class SocketHandler extends TextWebSocketHandler {

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {

    }

}

在客户端代码中,我尝试了所有可能的差异组合

在处理程序中使用 /socket

var websocket = new WebSocket("ws://localhost:8080/tsim/socket");
var websocket = new WebSocket("ws://localhost:8080/tsim/socket");
var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket");

在处理程序中使用 /rest/socket

var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket");

其中 tsim 是我的上下文根。一切都会抛出以下错误

WebSocket connection to 'ws://localhost:8080/tsim/socket' failed: Error during WebSocket handshake: Unexpected response code: 404.

我没有收到任何编译和部署错误。我用的是tomcat 8.0及以下的dispatcher servlet配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
       xmlns:batch="http://www.springframework.org/schema/batch"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
            http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/websocket
        http://www.springframework.org/schema/websocket/spring-websocket.xsd"
        >

    <import resource="classpath*:web-db-spring.xml"/>

     <context:component-scan base-package="com.traderslab.nseb,com.tlab.endpoint"/>

     <aop:aspectj-autoproxy/>

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="prefixJson" value="false"/>
                <property name="supportedMediaTypes" value="application/json"/>
                <property name="objectMapper">
                    <bean class="com.fasterxml.jackson.databind.ObjectMapper">
                        <property name="serializationInclusion" value="NON_NULL"/>
                    </bean>
                </property>
            </bean>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <mvc:default-servlet-handler/>

</beans>

下面是我的 web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/</url-pattern>
</servlet-mapping>

【问题讨论】:

  • 您在示例中显示ws://localhost:8080/tsim/socket,但错误消息显示ws://localhost:8080/TradeSim/socket。是tsim 还是TradeSim
  • 我尝试了不同的方法.. 我介绍的那部分将它作为 tsim 或 TradeSim 它的上下文根.. 为避免混淆,我将对其进行编辑

标签: java websocket spring-websocket


【解决方案1】:

我终于自己找到了答案。由于这是由调度程序 servlet 作为正常的 http 请求处理的。我需要在 WebSocketConfig 类中添加@Controller 注解

@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer

【讨论】:

  • 我正在使用 spring boot 并且有同样的问题,但是将 @Controller 添加到 WebSocketConfig 并没有帮助。我得到一个 403 令人惊讶。你能帮忙吗?
  • 找到了一个修复程序。如果你想用你的浏览器测试 websocket,你需要添加 registry.addHandler(socketHandler(),"/socket").setAllowedOrigins("*");
  • 请简要解释或编辑您的代码在何处使用此 registry.addHandler(socketHandler(),"/socket").setAllowedOrigins("*")
  • @Greyshack 仅当您的客户端从与服务器不同的域运行时才会出现这种情况。例如,如果您的客户端在 localhost:3000 上运行,而您的 spring 服务器在 localhost:8080 上,则您需要它,但如果您的 spring 服务器正在为客户端提供服务,则不需要
  • 这味道不对。 WebSocketConfig 如何成为请求处理程序?这就是@Controller 的意思。您现在拥有一个既是 Controller 原型又是 Configuration 原型的 bean。
【解决方案2】:

我的情况是,问题出在调度程序 servlet 映射中。我只有一个 *.mvc 映射,而 websocket 没有它,所以我不得不更改为:

  <servlet-mapping>
     <servlet-name>dispatcherServlet</servlet-name>
     <url-pattern>*.mvc</url-pattern>
     <url-pattern>/</url-pattern>
  </servlet-mapping>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 2020-02-27
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 2016-09-05
    相关资源
    最近更新 更多