The Spring 4 WebSocket integrates into a Spring MVC application

2.2k Views Asked by At
  1. I write the simple web application based on Spring MVC. Everything run Ok. Then I create Endpoint and HandShake class to Terminal (C# Application) communicate with Server via Websocket.

    • WebsocketEndPoint.java:

      public class WebsocketEndPoint extends BinaryWebSocketHandler {
      
          @Override
          public void afterConnectionEstablished(WebSocketSession session) throws Exception {
              System.out.println("After Connection Established !!!!");
          }
      
          @Override
          protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
              session.sendMessage(message);
          }
      
          @Override
          public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
              System.out.println("After Connection Closed !!!!");
          }
      
      }
      
    • HandshakeInterceptor.java:

       public class HandshakeInterceptor extends HttpSessionHandshakeInterceptor{
      
          @Override
          public boolean beforeHandshake(ServerHttpRequest request,
              ServerHttpResponse response, WebSocketHandler wsHandler,
      Map<String, Object> attributes) throws Exception {
              System.out.println("Before Handshake");
              return super.beforeHandshake(request, response, wsHandler, attributes);
          }
      
          @Override
          public void afterHandshake(ServerHttpRequest request,
      ServerHttpResponse response, WebSocketHandler wsHandler, Exception ex) {
              System.out.println("After Handshake");
              super.afterHandshake(request, response, wsHandler, ex);
          }
      }
      
    • XML to configure the websocket handler and interceptor : websocketconfig.xml

      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:websocket="http://www.springframework.org/schema/websocket"
          xsi:schemaLocation="
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/websocket
              http://www.springframework.org/schema/websocket/spring-websocket.xsd">
      
              <bean id="websocket" class="com.javahash.spring.WebsocketEndPoint"/>
      
              <websocket:handlers>
                  <websocket:mapping path="/PaymentHandler" handler="websocket"/>
                  <websocket:handshake-interceptors>
                  <bean class="com.javahash.spring.HandshakeInterceptor"/>
                  </websocket:handshake-interceptors>
              </websocket:handlers>
      </beans>
      
  2. Add websocketconfig.xml into Spring Application context:

    • web.xml:

      <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          version="2.4">
      
          <display-name>Web server</display-name>
      
          <servlet>
              <servlet-name>webserver</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <load-on-startup>1</load-on-startup>
          </servlet>
      
          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>/</url-pattern>
          </servlet-mapping>
      
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>
                  /WEB-INF/dispatcher-servlet.xml
                  /WEB-INF/websocketconf.xml
              </param-value>
          </context-param>
      
          <listener>
              <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
      </web-app>
      
  3. After adding websocketconfig.xml into Spring Application context:

    • Terminal (C# application) can communicate with Server via Socket but I can not access web.
    • I have WARNING: No mapping found for HTTP request with URI [Spring4MVCHelloWorld/hello]] in DispatcherServlet with name 'dispatcher'
    • dispatcher-servlet.xml:

      <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
      
          <context:component-scan base-package="com.javahash.spring.controller" />
      
          <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix">
                  <value>/WEB-INF/views/</value>
              </property>
              <property name="suffix">
                  <value>.jsp</value>
              </property>
          </bean>
      </beans>
      
    • HelloWorldController.java:

      package com.javahash.spring.controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      
      @Controller
      public class HelloWorldController { 
      
          @RequestMapping("/hello")
          public String hello(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
              model.addAttribute("name", name);
              return "helloworld";
          }
      
      }
      
    • It seems @Controller not effect.

Do i miss anything?

1

There are 1 best solutions below

0
On BEST ANSWER

I think you're missing <mvc:annotation-driven/> in your dispatcher-servlet.xml file. Spring scanned your Controller beans but Spring MVC did not register your mappings.