HttpInvoker远程调用实例

HttpInvoker是SpringFramework提供的远程通讯协议,只能用于JAVA程序间的通讯,且服务端和客户端必须

使用SpringFramework。

HttpInvoker 配置

服务端

web.xml

<!-- 远程服务 -->
  <servlet>
    <servlet-name>remote</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>WEB-INF/connectionInterface.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>remote</servlet-name>
    <url-pattern>/remote/*</url-pattern>
  </servlet-mapping>

connectionInterface.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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!-- 将接口声明为 HTTP invoker 服务 -->
    <bean name="userService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
        <property name="service" ref="userServiceImpl"/>
        <property name="serviceInterface" value="com.lhm.service.UserService"/>
    </bean>
    <bean id="userServiceImpl" class="com.lhm.service.impl.UserServiceImpl"/>
    
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props> 
                <prop key="/userService">userService</prop>               
            </props>
        </property>
    </bean>
</beans>

客户端

client.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
		
    <!--客户端所需要获得服务的bean信息  -->
    <bean name="userServiceInvoker" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <!--服务URL-->
        <property name="serviceUrl">
           <value>http://127.0.0.1:8080/sm/remote/userService</value>
        </property>
        <!--服务接口-->
        <property name="serviceInterface" value="com.lhm.service.UserService"/>
    </bean>
</beans>

测试验证

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lhm.service.UserService;
import com.lhm.value.UserInfoPage;

public class HttpInvokerClientTest {
	private static ApplicationContext atx = new ClassPathXmlApplicationContext(
			"client.xml");
	
	public static void main(String[] args) {
		UserService userService = (UserService) atx.getBean("userServiceInvoker");
		UserInfoPage userInfoPage = new UserInfoPage();
		userInfoPage.setPageSize(10);
		userInfoPage.setCurrentPage(1);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "蓝卿儿");
		List<Map<String, Object>> list = userService.listCustomerInfo(map);
		System.out.println("list="+list);
	}
}

注意事项:传入的实体类需要序列化。 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值