2014-02-19 31 views
17

我試圖運行彈簧web套接字的演示,但無法完全測試它。我使用的是Java 7和Tomcat 7.0.50。我在服務器啓動時沒有收到任何錯誤,但是當我用js打開連接到它的網頁時,我找不到404頁面。我不確定我是否缺少配置中的任何內容以使其運行,並且如何能夠從js端連接它。無法運行彈簧web套接字演示

我有以下XML文件:

<beans ....> 
    <context:annotation-config /> 

    <websocket:message-broker 
     application-destination-prefix="/app"> 
     <websocket:stomp-endpoint path="/hello"> 
      <websocket:sockjs /> 
     </websocket:stomp-endpoint> 
     <websocket:simple-broker prefix="/topic" /> 
    </websocket:message-broker> 
</beans> 

我的控制器類是:

@Controller 
public class SwsService { 
    @MessageMapping("/hello") 
    @SendTo("/topic/greetings") 
    public Greeting greeting(HelloMessage message) throws Exception { 
     return new Greeting("Hello, " + message.getName() + "!"); 
    } 

    public String getGreeting() { 
     return "Hello, you are in!"; 
    } 
} 

的JS從中我打電話是這樣的:

var sock = new SockJS("/hello"); 
sock.onopen = function() { 
    console.log("open"); 
}; 
sock.onclose = function() { 
    console.log("closed"); 
}; 
sock.onmessage = function (message) { 
    console.log("msg", message); 
}; 

控制檯輸出時我運行tomcat:

Feb 19, 2014 3:28:47 PM org.apache.catalina.core.AprLifecycleListener init 
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:/Program Files/Java/jre6/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files\TortoiseGit\bin;C:\Python24;C:\Program Files\TortoiseSVN\bin;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Users\harsh\AppData\Roaming\npm;E:\IDE\eclipse_indigo;;. 
Feb 19, 2014 3:28:47 PM org.apache.tomcat.util.digester.SetPropertiesRule begin 
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:SWS' did not find a matching property. 
Feb 19, 2014 3:28:48 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-bio-8080"] 
Feb 19, 2014 3:28:48 PM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["ajp-bio-8009"] 
Feb 19, 2014 3:28:48 PM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 1412 ms 
Feb 19, 2014 3:28:48 PM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service Catalina 
Feb 19, 2014 3:28:48 PM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/7.0.50 
Feb 19, 2014 3:28:51 PM org.apache.catalina.core.ApplicationContext log 
INFO: No Spring WebApplicationInitializer types detected on classpath 
Feb 19, 2014 3:28:51 PM org.apache.catalina.core.ApplicationContext log 
INFO: Initializing Spring root WebApplicationContext 
Feb 19, 2014 3:28:51 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization started 
Feb 19, 2014 3:28:51 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing Root WebApplicationContext: startup date [Wed Feb 19 15:28:51 IST 2014]; root of context hierarchy 
Feb 19, 2014 3:28:52 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/classes/conf/SwsContext.xml] 
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize 
INFO: Initializing ExecutorService 'clientInboundChannelExecutor' 
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize 
INFO: Initializing ExecutorService 'clientOutboundChannelExecutor' 
Feb 19, 2014 3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport initialize 
INFO: Initializing ExecutorService 'messageBrokerSockJsScheduler' 
Feb 19, 2014 3:28:53 PM org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler 
INFO: Mapped URL path [/hello/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler] 
Feb 19, 2014 3:28:53 PM org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup start 
INFO: Starting beans in phase 2147483647 
Feb 19, 2014 3:28:53 PM org.springframework.web.context.ContextLoader initWebApplicationContext 
INFO: Root WebApplicationContext: initialization completed in 2173 ms 
Feb 19, 2014 3:28:53 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 
Feb 19, 2014 3:28:53 PM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["ajp-bio-8009"] 
Feb 19, 2014 3:28:53 PM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 5842 ms 

瀏覽器控制檯輸出:

GET http://localhost:8080/hello/info 404 (Not Found) sockjs-0.3.min.js:27 
closed 

我怎樣才能成功地測試了嗎?


更新

我還試圖從這裏運行的組合例如:https://github.com/rstoyanchev/spring-websocket-portfolio通過@jhadesdev

的建議,但也於事無補。當我運行mvn tomcat7:run,我看到瀏覽器下面的輸出,並打開URL告訴404

[INFO] Scanning for projects... 
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethrea 
ded.SingleThreadedBuilder with a thread count of 1 
[INFO] 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building spring-websocket-portfolio 1.0.0-SNAPSHOT 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ spring-websocket-portfol 
io >>> 
[INFO] 
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ spring-web 
socket-portfolio --- 
[debug] execute contextualize 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory E:\libraries\spring-websocket-portfol 
io\src\main\resources 
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ spring-websoc 
ket-portfolio --- 
[INFO] No sources to compile 
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ spring-websocket-portfol 
io <<< 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ spring-websocket-portfol 
io --- 
[INFO] Running war on http://localhost:8080/spring-websocket-portfolio 
[INFO] Using existing Tomcat server configuration at E:\libraries\spring-websock 
et-portfolio\target\tomcat 
[INFO] create webapp with contextPath: /spring-websocket-portfolio 
Feb 27, 2014 10:20:46 AM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-bio-8080"] 
Feb 27, 2014 10:20:46 AM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service Tomcat 
Feb 27, 2014 10:20:46 AM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47 
Feb 27, 2014 10:20:49 AM org.apache.catalina.core.ApplicationContext log 
INFO: No Spring WebApplicationInitializer types detected on classpath 
Feb 27, 2014 10:20:49 AM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-bio-8080"] 

它卡在那裏,並沒有繼續。

+0

哈利你好。我認爲問題在於當你創建新的SockJS對象時,你會錯過你的項目的根。你可以嘗試var sock = new SockJS(「/ myProject/hello」);或使用c:url。 –

+0

@Evgeni @Evgeni我也嘗試過,但仍然是它的說法404::( –

+0

@HarryJoy - 你有沒有嘗試完整的網址? –

回答

2

嘗試在webapp中添加資產文件夾,其中所有stomp和sockjs.js文件都可用。看一下running application的這個例子,可以用mvn clean install jetty:run運行。

的問題似乎是,要麼sockjs不在服務器上或一些道路是錯誤的。基於鏈路上的例子,一跺腳端點可以configured like this

@EnableWebSocketMessageBroker 
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer { 
    @Override 
    public void registerStompEndpoints(StompEndpointRegistry registry) { 
     registry.addEndpoint("/portfolio").withSockJS(); 
    } 

} 

然後在JavaScript中的終點可以這樣調用:

var socket = new SockJS('/spring-websocket-portfolio/portfolio') 
var stompClient = Stomp.over(socket); 

其中/spring-websocket-portfolio是根部署路徑的應用。

+0

看到更新的問題。我嘗試過,但它不起作用。 :( –

1

你的應用程序看起來不錯。

你絕對應該前綴你的應用程序的根上下文JavaScript的請求,爲jhadesdev mentioned

您的應用程序是否註冊其他過濾器或自定義messageconverters?也許某些其他配置元素在此干擾。

最新的投資組合例子應該肯定的工作,這裏有一些問題:

  1. 你能告訴我們更多關於您的客戶端安裝程序(瀏覽器版本)?
  2. 您的瀏覽器是否支持websocket(測試它there)?
  3. 你還可以嘗試在隱身模式下(也許第三方瀏覽器擴展出現故障)?
  4. 您可以檢查您的瀏覽器是否使用本地主機請求的HTTP代理?
  5. 你能檢查這個HTTP 404是否在Tomcat訪問日誌中? (並看看你是否有這些或在catalina.out有趣的東西?)
+1

1.谷歌瀏覽器33 2.是3.試過但結果相同4.沒有它 –

+0

是的我嘗試在URL中添加根上下文的前綴,但也沒有幫助。: –

+0

剛剛編輯我的答案與第五項檢查。 –

9

我正面臨同樣的問題。這個線程給了我足夠的線索來解決這個問題。非常感謝。

SOLUTION: 如果你有一個Spring MVC調度的Servlet在web.xml配置並映射到URL模式如下圖所示

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/webui/*</url-pattern> 
</servlet-mapping> 

然後,你必須創建SockJS實例作爲下面

var socket = new SockJS('/contextPath/webui/hello'); 

注示:替換的contextPath其中y我們的應用上下文路徑

+0

幫助。謝謝。我注意到從Spring Boot jar文件運行的示例運行正常,但轉換爲war時確實需要上下文路徑。 –

1

您的DispatcherServlet如何映射? Here可能是你要找的東西