Search code examples
spring-mvcspring-securityspring-websocket

I can not access(error 404) to my spring mvc web app after adding spring websockets


After adding Spring web sockets (that works fine) to my existing spring mvc app I can not access to the app (Error 404 for all the app urls) but I can access to the socket fine. If I revert the changes that I added to spring-mvc.xml file and then deploy them, the app works fine. I'm not sure what I doing wrong, could you please help me?

I just was following this tutorial http://syntx.io/using-websockets-in-java-using-spring-4/ I'm using spring 4.

This is my Spring-mvc.xml

   <?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:websocket="http://www.springframework.org/schema/websocket"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
    <context:component-scan base-package="com.mobile.automation.view.controller"/>
    <context:annotation-driven/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="websocket" class="com.mobile.automation.sockets.WebsocketEndPoint"/>

    <websocket:handlers>
        <websocket:mapping path="/testing" handler="websocket"/>
        <websocket:handshake-interceptors>
            <bean class="com.mobile.automation.sockets.HandshakeInterceptor"/>
        </websocket:handshake-interceptors>
    </websocket:handlers>

</beans>

WebsocketEndPoint.java

import org.springframework.stereotype.Controller;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Controller
public class WebsocketEndPoint extends TextWebSocketHandler {

    @Override
    protected void handleTextMessage(WebSocketSession session,
            TextMessage message) throws Exception {
        super.handleTextMessage(session, message);
        TextMessage returnMessage = new TextMessage(message.getPayload()+" received at server");
        session.sendMessage(returnMessage);
    }

Solution

  • I don't see anything related to other Controllers or any MVC configuration in this example.

    Maybe you're missing something like this in your spring-mvc.xml file?

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <mvc:annotation-driven/>
    
    </beans>