web-dev-qa-db-ja.com

Spring Webソケットのデモを実行できません

Spring Web Socketのデモを実行しようとしていますが、完全にテストすることはできません。 Java 7とTomcat 7.0.50を使用しています。サーバーの起動中にエラーが発生しませんが、jsでWebページを開いて接続すると、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
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"]

そこにくっついて先に進みません。

19
Harry Joy

私はその方法でこの問題を解決しました:SockJSを作成するときに、次のようにlocalhost:8080 /を追加します

new SockJS('http://localhost:8080/spring-websocket-portfolio/portfolio')
0
tn2000

私にとっての解決策は、SockJSWebSocketConfigクラスで有効にすることでした:

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/search")
            .setAllowedOrigins("*")
            .setHandshakeHandler(new DefaultHandshakeHandler())
            .withSockJS();
}
0