7

In a working Spring MVC 4.0.2 project running under Tomcat 7.0.50, I'm trying to implement Spring Websocket with simple broker and sockjs stomp endpoint, following the official reference. So, in the contextConfigLocation (DistpacherServlet property) I've add a value "websocket-config.xml" which contains:

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

In eclipse console (catalina log) when I start Tomcat, appear the rows

  • Initializing ExecutorService 'clientInboundChannelExecutor'
  • Initializing ExecutorService 'clientOutboundChannelExecutor'
  • Initializing ExecutorService 'messageBrokerSockJsScheduler'
  • Mapped URL path [/monitor/**] onto handler of type [class org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler]

In the jsp page I put the code

. . . .
var socket = new SockJS("monitor");
var stompClient = Stomp.over(socket); . . . .

Unfortunately when I load the jsp page from Chrome (v32), I get this error:

Failed to load resource: the server responded with a status of 404 (Not Found)

http ://localhost:8080/(ProjectName)/monitor/info

Obviously I'm doing this test locally, then all other webapp resources are reachable at http ://localhost/(ProjectName) correctly.

What's wrong?

**** Update 20 Feb 2014 I've tried to follow this resolved stack question, which suggests to use Tomcat8 for Spring Websocket, unfortunately it doesn't work

Community
  • 1
  • 1
user3328277
  • 73
  • 1
  • 1
  • 4

2 Answers2

6

Any log output on the server side as a result of the request to /monitor/info? How is the DispatcherServlet itself mapped? I would expect to "/" from what you have above.

The SockJS endpoint is basically a URL mapped in Spring MVC (with a SimpleUrlHandlerMapping) so try other Spring MVC mapped controller methods. And turn up logging, could there be a filter or something else returning the 404?

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
  • WONDERFUL! :)) I've spent so much time trying to fix this problem and you solved it in a while. My mistake is the usage of Dispatcher url-pattern "*.do", so the Servlet doesn't catch websocket client request. I changed it in "/", (but I had to add others fake url pattern for resource files, as suggested by @Gofier [here](http://stackoverflow.com/questions/4169266/where-to-place-images-css-in-spring-mvc-app) and it worked! I love you :) Many thanks – user3328277 Feb 20 '14 at 15:19
  • Glad it's working now :) Keep in mind that for serving resource files we also have a resource handling mechanism. See http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-config-static-resources. – Rossen Stoyanchev Feb 20 '14 at 17:16
  • Could you expand on your answer a little? Are you saying the dispatcher servlet needs to also handle the requests for the SockJS endpoint? – Paul Feb 23 '14 at 07:06
  • Paul, yes the SockJS endpoint is an HTTP endpoint mapped with and served by Spring MVC. See SockJsHttpRequestHandler. – Rossen Stoyanchev Mar 05 '14 at 21:19
  • 4
    Hi there! I'm having the same issue, but unfortunately haven't used websockets for long and am wondering what you mean by changing things in "/"? Could you possibly walk me through what you had to do to fix the issue? – Theo Jul 01 '14 at 15:21
  • @RossenStoyanchev The endpoint is a static one like registry.addResourceHandler("/app/**").addResourceLocations("/"); or is it a servlet mappoing to add in protected String[] getServletMappings() { return new String[] { "/" }; } ? – Stephane Nov 14 '14 at 12:38
  • I also get a 404 on the ws://localhost:8080/nitro-project-rest/app/bts request with a mapping as: Mapped URL path [/app/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] – Stephane Nov 14 '14 at 13:18
  • @Theo In my case if I am not using STOMP and I am trying to connect using this url ws://localhost:8443/MyApp/call it doesn't work I get 404 for this, however I see another call fired from the page with http://localhost:8080/MyApp/call but the ws thing doesn't work, I do get and output "NoMapping found" and aslo in browser about: failed: Connection closed before receiving a handshake response – Bilbo Baggins Mar 28 '17 at 13:34
  • @RossenStoyanchev Hello sir, I have subscription mapping client side side code which subscribe to Channel, on first load page in ` – Shantaram Tupe Jun 16 '18 at 09:41
1

For those who struggle with this issue this might helps. I had following in web.xml:

<servlet>
 <servlet-name>rest</servlet-name> 
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping> 
 <servlet-name>rest</servlet-name> 
 <url-pattern>/rest/*</url-pattern> 
</servlet-mapping>

And had 404 response for this url: http ://localhost:8080/(ProjectName)/monitor

But this one works like charm: http ://localhost:8080/(ProjectName)/rest/monitor

Cheers!