Httpinvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的通讯,且服务端和客户端必须使用SpringFramework。
package test;
import java.util.Map;
/**
* @author zhuc
* @version 2012-5-18 下午1:19:08
*/
public interface IService {
/**
* @param msg
* @return
*/
public String getString(String msg);
/**
* @param map
* @return
*/
public Map<String, Object> getMap(String map);
/**
* @return
*/
public Car getCar();
}
package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author zhuc
* @version 2012-5-18 下午1:19:32
*/
public class ServiceImpl implements IService {
@Override
public String getString(String msg) {
return "hello : " + msg;
}
@Override
public Map<String, Object> getMap(String map) {
Map<String, Object> testMap = new HashMap<String, Object>();
testMap.put("m1", 1);
testMap.put("m2", "2");
List<String> list = new ArrayList<String>();
list.add("213");
list.add("456");
testMap.put("m3", list);
testMap.put(map, map);
System.out.println("远程调用:" + map);
return testMap;
}
/* (non-Javadoc)
* @see test.IService#getCar()
*/
@Override
public Car getCar() {
Car c = new Car();
c.setName("zhuc-httpinvoker-car");
c.setCapacity(100);
return c;
}
}
package test;
import java.io.Serializable;
/**
* @author zhuc
* @version 2012-5-17 下午10:29:18
*/
public class Car implements Serializable {
/**
*
*/
private static final long serialVersionUID = -1115598660168001267L;
private String name;
private transient Integer capacity;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the capacity
*/
public Integer getCapacity() {
return capacity;
}
/**
* @param capacity the capacity to set
*/
public void setCapacity(Integer capacity) {
this.capacity = capacity;
}
}
web.xml具体配置
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置该Servlet随应用启动时候启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet映射的url -->
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
remote-servlet.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 通过Spring HttpInvoker机制暴露远程访问服务 -->
<bean id="httpInvokerService" class="test.ServiceImpl" />
<bean name="/remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="httpInvokerService" />
<property name="serviceInterface" value="test.IService" />
</bean>
<!-- url映射 -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="remote.convert.service">/remoteService</prop>
</props>
</property>
</bean>
<!-- 此处将url映射为 remote.convert.service -->
</beans>
客户端
package test;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhuc
* @version 2012-5-18 下午1:22:39
*/
public class ClientTest {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"test/remote-client.xml");
IService service = (IService) applicationContext
.getBean("remoteService");
String msg = service.getString("zhuc-httpinvoker");
System.out.println(msg);
Car c = service.getCar();
System.out.println(c.getName());
System.out.println(c.getCapacity());
Map<String, Object> map = service.getMap("test");
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + "," + entry.getValue());
}
}
}
remote-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->
<bean id="remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="${url}" />
<property name="serviceInterface" value="test.IService" />
</bean>
<bean id="globalPropertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>test/client.properties</value>
</list>
</property>
</bean>
</beans>
新建一个client.properties文件,方便配置
#url=http://localhost:8080/HttpInvoker/remote/remoteService
url=http://localhost:8080/HttpInvoker/remote/remote.convert.service
输出结果如下:
hello : zhuc-httpinvoker
zhuc-httpinvoker-car
null
test,test
m1,1
m2,2
m3,[213, 456]
分享到:
相关推荐
里面有两个工程,一个模拟服务端,一个模拟客户端。演示了最近本的httpinvoker的使用
Hessian 、 HttpInvoker 、 XFire 、 Axis
Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和返回值是比较复杂的,通过httpInvoker有巨大的优势。 1. 远程访问流程 1) 服务端定义服务接口 2) 服务端实现服务接口 3) 暴露服务...
http invoker 在开发完http接口后可以做http post测试
和Hessian的轻量级传输协议不同的是,Spring HttpInvoker使用Java序列化来序列化参数和返回值,然后基于HTTP协议传输经序列化后的对象。当参数或返回值是复杂类型,并且不能通过Hessian的序列化机制序列化时,...
springboot-httpinvoker-demo.zip
公司内部讲义,比较了SOA,RMI和Spring HttpInvoker。并介绍了Spring HttpInvoker的基本使用方法。
Spring HttpInvoker,是一套基于Maven+Spring+SpringMVC+MyBatis框架,还包含了Invoker的客户端及服务器端的demo实例
Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的...
包含Jmail例子,AOP面向切面编程的日志处理例子,HttpInvoker的例子,RMI远程调用的例子,定时任务的例子,自己没事写的,简单易懂
模拟提交GET/POST,鞥呢返回详细头,cookies
可以方便的实现http消息调试,比控制台F12开发者工具中显示的更加详细透彻
NULL 博文链接:https://lggege.iteye.com/blog/369151
org.springframework.remoting.httpinvoker最基本的实现例子,这是3个eclipse的JavaEE工程,全部导入即可,能运行。 初学可以轻松了解Spring HttpInvoker 的结构和使用。
HTTP Invoker 网络请求测试程序,用于网络请求的测试,适用APP,小程序,API开发人员
NULL 博文链接:https://tou3921.iteye.com/blog/1729111
Hessian和Burlap都是基于HTTP的,他们都解决了RMI所头疼的防火墙渗透问题。但当传递过来的RPC消息中包含序列化对象时,RMI完胜... Spring的HttpInvoker,它基于HTTP之上提供RPC,同时又使用了Java的对象序列化机
Eclipse工程Http Invoker Service & Client
spring+httpInvoker 实例,含服务端和客户端代码,myEclipse工程,直接导入就可以测试,适合初学者. 提供 普通参数返回值和自定义对象返回值,两个方式.