mardi 23 juin 2015

The Spring 4 WebSocket integrates into a Spring MVC application

  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://ift.tt/GArMu6"
          xmlns:xsi="http://ift.tt/ra1lAU"
          xmlns:websocket="http://ift.tt/1heCnO0"
          xsi:schemaLocation="
              http://ift.tt/GArMu6
              http://ift.tt/1jdM0fG
              http://ift.tt/1heCnO0
              http://ift.tt/1CzG6NS">
      
              <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://ift.tt/qzwahU"
          xmlns:xsi="http://ift.tt/ra1lAU"
          xsi:schemaLocation="http://ift.tt/qzwahU http://ift.tt/16hRdKA"
          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://ift.tt/GArMu6"
       xmlns:context="http://ift.tt/GArMu7"
       xmlns:xsi="http://ift.tt/ra1lAU"
       xsi:schemaLocation="
       http://ift.tt/GArMu6
       http://ift.tt/1cnl1uo
       http://ift.tt/GArMu7
       http://ift.tt/1ldEMZY">
      
          <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?

Aucun commentaire:

Enregistrer un commentaire