Java学习之路-Spring的HttpInvoker学习

摘要:
然而,当传入的RPC消息包含序列化对象时,RMI胜过Hessian和Burlap。Spring开发团队意识到RMI服务和基于HTTP的服务之间的差距,Spring的HttpInvoker应运而生。Spring的HttpInvoker提供基于HTTP的RPC,也使用Java的对象序列化机制。首先,我们创建一个实体类并实现Serializable接口packageentity;importjava.io.Serializable;PublicclassFruitimplementsSerializable{privateStringname;privateStringcolor;publicStringgetName(){returnname;}publicvoidsetName{this.name=name;}publicStringgetColor()}returncolor;}public voidsetColor{this.color=color;}}第二步,创建接口packageservice;importjava.util.List;进口实体.水果;PublicinterfaceFruitService{List<Fruit>getFruitList();}3。在步骤2中创建一个类并实现接口packageservice.impl;importjava.util.ArrayList;importjava.util.List;importservice.FruitService;进口实体.水果;PublicclassFruitServiceImplementsFruitService{publicList<Fruit>getFruitList(){List<Fruct>List=newArrayList<Frut();Fruitf1=newFruit();f1.setName;f1.setColor;Fruitf2=newFrute();f2.setName;f2.setColor;f2.setColo;List.add;List.aadd;returnlist;}}}}在web-INF<contextparam name>contextConfigLocation</param name>下的web.xml中配置SpringMVC所需的信息value>classpath:applicationContext。xml</param value></context param><listener><监听器类>org。弹簧框架。网状物所容纳之物ContextLoaderListenerspringMvcorg。弹簧框架。网状物servlet。Dispatch CherServlet</servlet class><启动时加载>1</启动时加载</servlet><servlet mapping><servlet name>springMvc</servlet name><url pattern>/</url pattern></servlet mapping>V配置要在applicationContext.xml中导出的服务的bean信息=“org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter”p:serviceInterface=“service.FruitService”p:service ref=“fultService”/˃VI。在WEB-INF下创建springMvc-servlet.xml文件,并配置urlMapping˂?

Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题。但当传递过来的RPC消息中包含序列化对象时,RMI就完胜Hessian和Burlap了。
因为Hessian和Burlap都是采用了私有的序列化机制,而RMI使用的是Java本身的序列化机制。如果数据模型非常复杂,那么Hessian/Burlap的序列化模型可能就无法胜任了。
Spring开发团队意识到RMI服务和基于HTTP的服务之前的空白,Spring的HttpInvoker应运而生。
Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机制。

程序的具体实现
一、首先我们创建一个实体类,并实现Serializable接口

package entity;
import java.io.Serializable;
public class Fruit implements Serializable {
    private String name;
    private String color;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
}

二、创建一个接口 

package service;
import java.util.List;
import entity.Fruit;
public interface FruitService {
    List<Fruit> getFruitList();
}

三、创建一个类,并实现步骤二中的接口 

package service.impl;
import java.util.ArrayList;
import java.util.List;
import service.FruitService;
import entity.Fruit;
public class FruitServiceImpl implements FruitService {
    public List<Fruit> getFruitList() {
        List<Fruit> list = new ArrayList<Fruit>();
        Fruit f1 = new Fruit();
        f1.setName("橙子");
        f1.setColor("黄色");
        Fruit f2 = new Fruit();
        f2.setName("苹果");
        f2.setColor("红色");
        list.add(f1);
        list.add(f2);
        return list;
    }
}

四、在WEB-INF下的web.xml中配置SpringMVC需要的信息 

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping> 

五、在applicationContext.xml配置需要导出服务的bean信息 

<bean id="furitService" class="service.impl.FruitServiceImpl"></bean>
<bean id="FuritService"
    class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"
    p:serviceInterface="service.FruitService" p:service-ref="furitService" /> 

六、在WEB-INF下创建springMvc-servlet.xml文件,并配置urlMapping 

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/fruitService">FuritService</prop>
            </props>
        </property>
    </bean>
</beans> 

七、在applicationContext.xml编写客户端所需要获得服务的bean信息 

<bean id="getFruitService"
    class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
    p:serviceInterface="service.FruitService"
    p:serviceUrl="http://localhost:8080/SpringHttpInvoker/fruitService" /> 

八、编写测试代码 

package test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import entity.Fruit;
import service.FruitService;
public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        FruitService fruitService = (FruitService) ctx
                .getBean("getFruitService");
        List<Fruit> fruitList = fruitService.getFruitList();
        for (Fruit fruit : fruitList) {
            System.out.println(fruit.getColor() + "的" + fruit.getName());
        }
    }
}

将项目部署到Tomcat上,启动Tomcat服务,并运行测试代码
===========控制台========

黄色的橙子
红色的苹果

=======================

免责声明:文章转载自《Java学习之路-Spring的HttpInvoker学习》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇IOS学习:UITableView使用详解3 分组表的简单使用使用容器化块存储OpenEBS在K3s中实现持久化存储下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

框架dubbox的简单使用

之前: RPC: Remote Produedure Call :一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议 SOA: Service-oriented architecture面向服务的体系结构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)通过这些服务之间定义良好的接口和契约联系起来。接口是采用中立的方式进行定...

HttpServletResponse、页面文件下载、验证码

summarize 在创建Servlet时,会覆盖service方法或doGet()/doPost(),它俩都是 HTTPServletresponse这些方法都有俩参数,一个是请求request,一个是响应response,HttpServletResponse是ServletResponset的子接口, 浏览器发出http请求,会直接传给Tomcat,...

nc的基本用法

       nc(netcat) 被誉为网络安全界的‘瑞士军刀’,可以用于完成几乎涉及TCP、UDP或者Unix域套接字的任何事。它可以打开TCP连接,发送UDP报文,在任意的TCP和UDP端口监听,进行端口扫描,支持ipv6。不象telnet,nc能够更好地支持脚本,能够将错误消息分离到标准错误,而不是标准输出。nc有四种典型应用: 一、C/S模型 用...

kafka 基础知识梳理-kafka是一种高吞吐量的分布式发布订阅消息系统

一、kafka 简介 今社会各种应用系统诸如商业、社交、搜索、浏览等像信息工厂一样不断的生产出各种信息,在大数据时代,我们面临如下几个挑战: 如何收集这些巨大的信息 如何分析它 如何及时做到如上两点 以上几个挑战形成了一个业务需求模型,即生产者生产(produce)各种信息,消费者消费(consume)(处理分析)这些信息,而在生产者与消费者之间,需要...

kafka消息的分发与消费

关于 Topic 和 Partition:   Topic: 在 kafka 中,topic 是一个存储消息的逻辑概念,可以认为是一个消息集合。每条消息发送到 kafka 集群的消息都有一个类别。物理上来说,不同的 topic 的消息是分开存储的,每个 topic 可以有多个生产者向它发送消息,也可以有多个消费者去消费其中的消息。   Partition...

Servlet第三篇【request和response简介、response的常见应用】

response、request对象 Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象、和代表响应的response对象 既然request对象代表http请求,那么我们获取浏览器提交过来的数据,找request对象即可。response对象代表http响应,那么我们向浏览器输出数据,找response对象...