无法运行spring web socket演示

我正在尝试运行spring web socket的演示,但无法完全测试它。 我使用的是java 7和tomcat 7.0.50。 我在服务器启动时没有收到任何错误,但是当我用js打开网页连接到它时,我找不到404页面。 我不确定我是否缺少任何配置以使其运行,我怎么能从js端连接它。

我有以下xml文件:

         

我的控制器类是:

 @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"] 

它停留在那里,不会继续。

我遇到了同样的问题。 这个post给了我足够的线索来解决这个问题。 非常感谢。

解决方案:如果您在web.xml中配置了Spring MVC Dispatcher Servlet并映射到url-pattern,如下所示

  dispatcher /webui/*  

然后你必须创建SockJS实例,如下所示

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

注意:contextPath替换为您的应用程序上下文路径。

尝试在webapp中添加assets文件夹,其中所有stomp和sockjs.js文件都可用。 看一下正在运行的应用程序的示例,可以使用mvn clean install jetty:run

问题似乎是sockjs不在服务器上或某些路径是错误的。 根据链接上的示例,可以像这样配置 stomp端点:

 @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是应用程序的根部署路径。

此 Chrome扩展程序可以帮助您测试Websockets。

您还可以查看我使用Spring Boot with Websockets的简单聊天项目。

你的应用看起来很好。

正如jhadesdev所提到的 ,你肯定应该将JavaScript请求添加到应用程序的根上下文中 。

您的应用程序是否注册了其他filter或自定义消息转换器? 也许其他一些配置元素在这里干扰。

最新的投资组合示例应该肯定有效,这里有一些问题:

  1. 您能否告诉我们有关您的客户端设置(浏览器,版本)的更多信息?
  2. 您的浏览器是否支持websocket(在那里测试)?
  3. 您是否也可以尝试隐身模式(可能是第三方浏览器扩展程序有问题)?
  4. 你能检查一下你的浏览器没有使用HTTP代理来进行localhost请求吗?
  5. 你能检查一下这个HTTP 404是否在Tomcat访问日志中吗? (看看你是否在那些或catalina.out中有一些有趣的东西?)

你的DispatcherServlet是如何映射的? 这可能是你正在寻找的