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);
}
}
注意事项:传入的实体类需要序列化。