2015-01-17 159 views
5

我想創建一個websocket端點與春天。但每當我試圖從客戶端連接,我得到了404。我也有一個Java實現與春天websocket 404找不到

@ServerEndpoint(value = "/websocket") 

它工作良好。下面是我的春天實施代碼不起作用。我在這裏做錯了什麼?

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的可能的組合

與/插座在處理程序

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"); 

與/休息/插座in handler

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> 
+0

你給'WS://本地主機:8080 /尖沙咀/ socket'在你的例子,但錯誤信息顯示'ws:// localhost:8080/TradeSim/socket'。它是'tsim'還是'TradeSim'? – Bogdan

+0

我嘗試了不同的方式..我將其作爲tsim或TradeSim將其作爲contextroot進行覆蓋。爲避免混淆,我將對其進行編輯 – anandaravindan

回答

2

我終於找到了自己的答案。由於這是由調度程序servlet作爲普通的http請求處理的。我需要註解@Controller添加到WebSocketConfig類

@Configuration 
@EnableWebSocket 
@Controller 
public class WebSocketConfig implements WebSocketConfigurer 
+0

我使用的是spring引導並具有相同的問題,但是向WebSocketConfig添加@Controller doesn'幫助。令人驚訝的是我得到了403分。你能幫我嗎? – Greyshack

+0

找到修復程序。如果你想用瀏覽器測試websocket,你需要添加registry.addHandler(socketHandler(),「/ socket」)。setAllowedOrigins(「*」); – Greyshack

0

我我的情況下,問題是調度servlet映射。我只考了* .mvc映射和WebSocket的是沒有了,所以我不得不改變:

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