无法运行spring web socket演示 [英] Not able to run spring web socket demo

查看:141
本文介绍了无法运行spring web socket演示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有以下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 
公共类SwsService {
@MessageMapping(/ hello)
@SendTo(/ topic / greetings)
公共问候语greeting(HelloMessage消息)抛出异常{
返回新的Greeting(Hello,+ message.getName()+!);
}

public String getGreeting(){
return你好,你在!;
}
}

我称之为的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时的控制台输出:

  2014年2月19日下午3:28:47 org.apache.catalina.core.AprLifecycleListener init 
INFO:基于APR的Apache Tomcat Native库,可实现最佳性能在java.library.path上找不到生产环境:C:\Program Files \ Java * \\ jre7 \ 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 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ C:\Program Files \\\
odejs \; C:\Program Files \ Git \ cm d; C:\Users\harsh\AppData\Roaming\\\
pm; E:\IDE\eclipse_indigo ;;。
2014年2月19日下午3:28:47 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告:[SetPropertiesRule] {Server / Service / Engine / Host / Context}设置属性'source 'to'org.clipse.jst.jee.server:SWS'找不到匹配的属性。
2014年2月19日下午3:28:48 org.apache.coyote.AbstractProtocol init
INFO:初始化ProtocolHandler [http-bio-8080]
2014年2月19日3:28 :晚上48点org.apache.coyote.AbstractProtocol init
INFO:初始化ProtocolHandler [ajp-bio-8009]
2014年2月19日下午3:28:48 org.apache.catalina.startup。 Catalina load
INFO:初始化处理时间为1412 ms
2014年2月19日下午3:28:48 org.apache.catalina.core.StandardService startInternal
INFO:启动服务Catalina
2014年2月19日下午3:28:48 org.apache.catalina.core.StandardEngine startInternal
INFO:启动Servlet引擎:Apache Tomcat / 7.0.50
2014年2月19日下午3:28:51 org.apache.catalina.core.ApplicationContext log
INFO:没有在类路径上检测到Spring WebApplicationInitializer类型
2014年2月19日下午3:28:51 org.apache.catalina.core.ApplicationContext log
INFO:初始化Spring root WebApplicationContext
2014年2月19日下午3:28:51 org.springframework.web.context.ContextLoader initWe bApplicationContext
INFO:Root WebApplicationContext:初始化已启动
2014年2月19日下午3:28:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO:刷新根WebApplicationContext:启动日期[Wed 2月19日15:28:51 IST 2014];上下文层次结构的根源
2014年2月19日下午3:28:52 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO:从ServletContext资源加载XML bean定义[/ WEB-INF / classes /conf/SwsContext.xml]
2014年2月19日下午3:28:53 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport初始化
INFO:初始化ExecutorService'clientInboundChannelExecutor'
2014年2月19日3:28:53 PM org.springframework.scheduling.concurrent.ExecutorConfigurationSupport初始化
INFO:初始化ExecutorService'clientOutboundChannelExecutor'
2014年2月19日下午3:28:53 org.springframework.scheduling.concurrent.ExecutorConfigurationSupport初始化
INFO:初始化ExecutorService'messageBrokerSockJsScheduler'
2014年2月19日下午3:28:53 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO:映射的URL路径[/ hello / **]到[class org.springframe]类型的处理程序work.web.socket.sockjs.support.SockJsHttpRequestHandler]
2014年2月19日下午3:28:53 org.springframework.context.support.DefaultLifecycleProcessor $ LifecycleGroup start
INFO:在阶段2147483647 $中启动bean b $ b 2014年2月19日下午3:28:53 org.springframework.web.context.ContextLoader initWebApplicationContext
INFO:Root WebApplicationContext:初始化在2173 ms完成
2014年2月19日3:28:53 PM org.apache.coyote.AbstractProtocol start
INFO:启动ProtocolHandler [http-bio-8080]
2014年2月19日下午3:28:53 org.apache.coyote.AbstractProtocol start
INFO:启动ProtocolHandler [ajp-bio-8009]
2014年2月19日下午3:28:53 org.apache.catalina.startup.Catalina启动
INFO:服务器启动时间为5842 ms

浏览器控制台输出:

  GET http:// localhost:8080 / hello / info 404(未找到)sockjs-0.3.min.js:27 
关闭

我如何成功测试它?






更新



我也试过从这里运行投资组合示例: https://github.com/rstoyanchev/spring-websocket-portfolio

但它也无济于事。当我运行 mvn tomcat7:run 时,我看到以下输出和浏览器上的打开url告诉404。

  [INFO]扫描项目... 
[INFO]
[INFO]使用构建器org.apache.maven.lifecycle.internal.builder.singlethrea
ded.SingleThreadedBuilder,线程数为1
[INFO]
[INFO] --------------------------- ---------------------------------------------
[ INFO]建立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:资源(默认资源)@ spring-web
socket-portfolio ---
[ debug] execute contextualize
[INFO]使用'UTF-8'编码复制过滤后的资源。
[INFO]跳过不存在的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]没有资源来编译
[ 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]在http:// localhost:8080 / spring-websocket-portfolio上运行战争
[INFO]在E:\ libraries\spring-websock使用现有的Tomcat服务器配置
et-portfolio \ target \ tomcat
[INFO]使用contextPath创建webapp:/ spring-websocket-portfolio
2014年2月27日上午10:20:46 org.apache.coyote.AbstractProtocol init
信息:初始化ProtocolHandler [http-bio-8080]
2014年2月27日上午10:20:46 org.apache.catalina.core.StandardService startInternal
INFO:启动服务Tomcat
2月27日,2014 10:20:46 AM org.apache.catalina.core.StandardEngine startInternal
INFO:启动Servlet引擎:Apache Tomcat / 7.0.47
2014年2月27日上午10:20:49组织。 apache.catalina.core.ApplicationContext log
INFO:没有在类路径上检测到Spring WebApplicationInitializer类型
2014年2月27日上午10:20:49 org.apache.coyote.AbstractProtoc ol start
INFO:启动ProtocolHandler [http-bio-8080]

卡住了那里并没有继续。

解决方案

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



解决方案:
如果您在web.xml中配置了Spring MVC Dispatcher Servlet并映射到如下所示的网址格式

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

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

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

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


I am trying to run a demo of spring web socket but not able to test it completely. I am using java 7 and tomcat 7.0.50. I don't get any error while server startup, but when I open the web page with js making the connection to it I got 404 page not found. I am not sure if I am missing anything in configuration to make it run and how can I be able to connect it from js side.

I have following xml file:

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

My controller class is:

@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!";
    }
}

The js from which I am calling this is:

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

The console output when I run the 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

The browser console output:

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

How can I successfully test it?


Updates

I also tried running the portfolio example from here: https://github.com/rstoyanchev/spring-websocket-portfolio as suggested by @jhadesdev

But it also doesn't help. When I run mvn tomcat7:run, I see following output and open url on browser tells 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"]

It stuck there and doesn't go ahead.

解决方案

I was facing the same problem. This thread gave me enough clues to solve the problem. Thanks a lot.

SOLUTION: In case you have a Spring MVC Dispatcher Servlet configured in your web.xml and mapped to a url-pattern as shown below

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

Then you have to create the SockJS instance as shown below

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

NOTE: Replace contextPath with your application context path.

这篇关于无法运行spring web socket演示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆