Search code examples
javaspring-mvcstompspring-websocketsockjs

How to configure Spring 4 WebSocket with sockjs using xml configuration


Hi my current websocket configuration is.

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

and my url pattern configuration is

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

if i change to pattern to '/' then it works fine else the give an error

GET http://localhost:8080/demoapp/chat/info 404 (Not Found)

what is wrong please suggest.

thanks


Solution

  • When using the *.html pattern, you're only mapping those requests to the DispatcherServlet - all other requests (in this case, all SockJS/websocket/etc requests) are ignored and an HTTP 404 error is returned by the servlet container.

    So yes, you should be using "/" as a mapping pattern otherwise it won't work.

    It can be hard to start writing a websocket application - a lot of new concepts and things to pay attention to. But the programming model is actually quite close to Spring MVC.

    Here are a few pointers to help you: