HttpInvoker通过HTTP方式实现远程调用,把数据通过二进制的方式发送给远程客户端,因此有涉及到自定义的JavaBean的时候,需要实现Serializable接口。
1、定义JavaBean用于传输
public class Cip
implements Serializable {
private static final long serialVersionUID = -9202513333895834316L;
/** ID */
private long id;
/** 正书名 */
private String firstBookName;
//其他字段以及对应的setter、getter方法......
}
2、定义服务接口以及对应的实现类
public interface CipService {
public List<Cip> queryCip(int start,int rows);
public int getCipCount();
}
@Service("cipService")
public class CipServiceImpl implements CipService {
@Resource private CipDao cipDao;
public List<Cip> queryCip(int start, int rows) {
return cipDao.queryCip(start, rows);
}
public int getCipCount() {
return cipDao.getCipCount();
}
}
3、在web.xml加入spring mvc相关的配置
<servlet>
<servlet-name>video</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>video</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<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>
4、在applicationContext.xml文件中配置HttpInvokerServiceExporter和SimpleUrlHandlerMapping
<!-- 配置HttpInvokerServiceExporter -->
<bean id="httpService" class=
"org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="cipService"/>
<property name="serviceInterface" value="com.tcsoft.video.service.CipService"/>
</bean>
<!-- 配置SimpleUrlHandlerMapping -->
<bean class="
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/httpService">httpService</prop>
</props>
</property>
</bean>
其中HttpInvokerServiceExporter是用于暴露服务端上的服务接口,而SimpleUrlHandlerMapping是用于匹配http请求的,也就是说客户端发送的http请求必须匹配/httpService.
5、把相关类,比如上面的Cip.java,CipService.java打成jar包给客户端程序,或者直接把两个类拷贝给客户端,这里需要注意的是,报名需要一直。以此采用前者的方式比较好。
-----------------------------------------------------------------------------------------------------------
客户端的调用方式:
1、在applicationContext.xml文件中配置HttpInvokerProxyFactoryBean
<!-- 配置HttpInvokerProxyFactoryBean -->
<bean id="cipService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://192.168.1.217:7788/ndcnc/httpService"/>
<property name="serviceInterface" value="com.tcsoft.video.service.CipService"/>
</bean>
其中serviceUrl是HTTP请求地址,如上面的:http://192.168.1.217:7788/ndcnc/httpService,
http://192.168.1.217:7788/ndcnc是服务器地址,/httpService用于匹配服务端定义的匹配方式。
2、编写单元测试
@Test
public void testHttpInvoker() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
CipService cipService = (CipService) ac.getBean("cipService");
System.out.println(cipService.getCipCount());
List<Cip> cipList = cipService.queryCip(0, 2);
for(Cip cip : cipList) {
System.out.println("id:" + cip.getId() + " firstBookName:" + cip.getFirstBookName());
}
}
//打印结果是:~
266
id:101 firstBookName:与安全同行
id:102 firstBookName:与健康相伴