java 初试利用spring开发接口(tomcat + netty + springmvc) - Go语言中文社区

java 初试利用spring开发接口(tomcat + netty + springmvc)


需求:完成一个URL形式的接口。
在URL中请求数据,服务器中转数据到目标服务器A,返回从A中获取的返回值。

开发工具:myeclipse, tomcat-7.0,
项目结构:
引用的spring包

webroot目录结构

步骤:
1.用myeclipse引用spring3.1框架依赖;
2.编写web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>projectnamenamename</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>servlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>


</web-app>

3.可以看出对servlet-context.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.xxx.xx.*" />
<!-- 这个必须包含所有的package文件, 否则会可能会出现进不了Controler的错误!!!-->  
  <!--  <mvc:default-servlet-handler /> 
     Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven/>
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!-- <context:component-scan base-package="com.fanews.tn.action.*"/> -->
</beans>

4.编写控制器–@部分别丢了

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class TNController {
/**
 * 控制器
 */
    @Autowired
    TNService tnService;//引用服务器类

    @RequestMapping("/hello")
    public String hello(){
        tnService.justSend();
        return "hello";
    }
}

5.编写服务器类–@部分别丢了

import org.springframework.stereotype.Service;

@Service
public class TNService {
/**
 * 处理器
 */
    public void justSend(){
        try {
            ClientReq req = new ClientReq();
            Client client = new Client("xxx.xxx.xxx.xxx", 9998);
            client.connect(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

6.编写netty处理类。–客户端
·Client
·ClientHandler
·MarshallingCodeCFactory

7.于是可以作为一个web项目放到tomcat里面进行运行/调试了。
URL:localhost:8080/”projectName”/hello.action

期间碰到的问题及解决:
Client发送消息,但是没有得到Server的响应
分析:
在编程上的编码格式是没有问题的Marshalling;
在小程序单跑是能得到响应的;
方案:
1.调试程序,研究比较tomcat和单跑 数据的差异;
2.抓包进行分析。
@
选择方案1-由于走TCP,用wireshark进行获取,发现Client都发送了数据,但是bytes不同(或许有其他的差异,但是目前只能从中看懂这个),Server端单跑接收到数据,但是tomcat端接收不到;
@
于是,继续做一个单跑的程序进行测试。换了包名Client发送的bytes不同,而且也无法得到反馈——发现 包名的不同造成的结果

事后:
不知道包名为什么会混到Client发送的信息里,要知道原因得去研究Netty源码了吧。。。
(很佩服前辈能在调试Netty源码的时候找到数据不同的关键方法,真是大神啊。。)

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u013244192/article/details/69666751
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 18:43:45
  • 阅读 ( 965 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢