未找到404的Spring websocket

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了未找到404的Spring websocket相关的知识,希望对你有一定的参考价值。

我正在尝试用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) {

    }

}

在客户端代码我尝试了所有diff可能的组合

在处理程序中使用/ 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及以下版本是调度程序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>
答案

我终于找到了答案。因为这是由调度程序servlet作为普通的http请求来处理的。我需要将@Controller注释添加到WebSocketConfig类

@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer
另一答案

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

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

以上是关于未找到404的Spring websocket的主要内容,如果未能解决你的问题,请参考以下文章

RabbitMQ WebSocket 404 未找到

HTTP 状态 404 - 在 Spring Boot 中调用 url 时未找到

Spring-Boot MVC 模板未加载(未找到 404)

Spring安全性j_spring_security_check调用给出404未找到错误[关闭]

spring-boot 基本 JSP 404 未找到

Spring Boot /actuator 返回 404 未找到