java 找不到Spring websocket获取404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28000900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring websocket getting 404 not found
提问by anandaravindan
I am trying to create an websocket endpoint with spring. But Whenever I am trying to connect from the client end, I get 404. I also have an Java implementation with
我正在尝试使用 spring 创建一个 websocket 端点。但是每当我尝试从客户端连接时,我都会收到 404。我也有一个 Java 实现
@ServerEndpoint(value = "/websocket")
which works good. Below is the code of my spring implementation which is not working. What am I doing wrong here?
这很好用。下面是我的 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();
}
}
below is the Handler
下面是处理程序
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) {
}
}
In the client side code I tried all diff possible combination
在客户端代码中,我尝试了所有不同的可能组合
with /socket in handler
在处理程序中使用 /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");
with /rest/socket in handler
在处理程序中使用 /rest/socket
var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket");
where tsim is my contextroot. Everything throws the below error
其中 tsim 是我的上下文根。一切都会引发以下错误
WebSocket connection to 'ws://localhost:8080/tsim/socket' failed: Error during WebSocket handshake: Unexpected response code: 404.
I didn't get any compilation and deployment errors. I used tomcat 8.0 and below is the dispatcher servlet configuration.
我没有收到任何编译和部署错误。我用的是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.MappingHymanson2HttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
<property name="objectMapper">
<bean class="com.fasterxml.Hymanson.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>
below is my web.xml
下面是我的 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>
采纳答案by anandaravindan
I finally found the answer myself. Since this is handled by dispatcher servlet as normal http request. I need to add @Controller annotation to the WebSocketConfig class
我终于自己找到了答案。由于这是由调度程序 servlet 作为普通 http 请求处理的。我需要在 WebSocketConfig 类中添加 @Controller 注释
@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer
回答by Krystian
I my case, the problem was in dispatcher servlet mapping. I had a *.mvc mapping only, and websocket was without it, so I had to change to:
我的情况是,问题出在调度程序 servlet 映射中。我只有一个 *.mvc 映射,而 websocket 没有它,所以我不得不更改为:
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.mvc</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>