Ich versuche, einen Websocket Endpunkt mit Feder zu erstellen. Aber immer, wenn ich von der Client-Seite zu verbinden versuchen, erhalte ich 404. Ich habe auch eine Java-Implementierung mitSpring Websocket bekommen 404 nicht gefunden
@ServerEndpoint(value = "/websocket")
, die gut funktioniert. Unten ist der Code meiner Spring-Implementierung, der nicht funktioniert. Was mache ich hier falsch?
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();
}
}
unten ist der 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) {
}
}
Im Code Client-Seite I alle möglichen Kombinationen
mit/Buchse in handler
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");
mit/rest/socket diff versucht in Handler
var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket");
wo tsim ist mein contextroot. Alles wirft den folgenden Fehler
Ich habe keine Kompilierungs- und Bereitstellungsfehler erhalten. Ich habe Tomcat 8.0 und darunter die Dispatcher-Servlet-Konfiguration verwendet.
<?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>
unten ist mein 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>
Sie zeigen 'ws: // localhost: 8080/tsim/socket' in Ihre Beispiele, aber die Fehlermeldung zeigt 'ws: // localhost: 8080/TradeSim/socket' an. Es ist 'tsim' oder' TradeSim'? – Bogdan
Ich habe es anders versucht .. den Teil, den ich behandelt habe, nehme es als tsim oder TradeSim es ist der contextroot .. Um Verwirrung zu vermeiden, bearbeite ich es – anandaravindan