前一段时间在做caredog gis系统的时候,需要做一个统计模块,让另外一个web系统调用,自己的项目是用ssh框架搭建的.想了想还是用webservice吧.
利用webservice可以使不同的项目相互之间通信.而你只对外提供一个接口即可.
例如:我需要将下面类中的方法提供给另一个系统调用.
package com.tiros.caredog.cd.service;
import java.util.List;
import com.tiros.caredog.cd.pojo.Report;
public interface ReportService {
List<Report> queryByCondition(Integer year, Integer month, Integer version);
int add(int a , int b);
}
大家可以看到. 在这个接口中有两个方法. 一个是传入年,月,版本号来查询report的list.
另一个是一个简单的加法运算. 用来测试的.
step1:导入webservice需要的jar包. (jar包请斟酌删减,可能与你的项目中其他jar包有冲突的,或者用不到的可以删减)
activation-1.1.jar commons-beanutils-1.7.0.jar commons-codec-1.3.jar commons-logging-1.0.4.jar jaxws-api-2.0.jar jaxen-1.1-beta-9.jar jdom-1.0.jar mail-1.4.jar saaj-api-1.3.jar saaj-impl-1.3.jar spring-1.2.6.jar stax-api-1.0.1.jar wsdl4j-1.6.1.jar wstx-asl-3.2.0.jar xbean-2.2.0.jar xbean-spring-2.8.jar xfire-aegis-1.2.6.jar xfire-annotations-1.2.6.jar xfire-core-1.2.6.jar xfire-java5-1.2.6.jar xfire-jaxws-1.2.6.jar xfire-spring-1.2.6.jar xfire-jsr181-api-1.0-M1.jar jsr173_api-1.0.jar XmlSchema-1.1.jar
step2: 在web.xml中配置如下信息.(因为我的项目用了spring管理.)<servlet>标签一定要放在listener之后
<servlet> <servlet-name>xfire</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>xfire</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
配置一个叫 xfire的servlet . 当客户端用/services/请求时调用.
step3:在web-info下新增一个xfire-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>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 定义访问的url-->
<!-- 使用XFire导出器 -->
<bean id="baseWebService"
class="org.codehaus.xfire.spring.remoting.XFireExporter"
lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定义的工厂 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire实例 -->
<property name="xfire" ref="xfire" />
</bean>
<bean id="EntrustService" parent="baseWebService">
<!-- 业务服务bean "reportServiceImp"为applicationcontext.xml中的"reportServiceImp"-->
<property name="serviceBean" ref="reportServiceImp" />
<!-- 业务服务bean的窄接口类 serviceClass. 接口类.-->
<property name="serviceClass"
value="com.tiros.caredog.cd.service.ReportService" />
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/reportService">
<ref bean="EntrustService" />
</entry>
</map>
</property>
</bean>
</beans>
在上面的代码片段中的reportServiceImp.来源于applicationContext.xml中的bean.
<bean id="reportServiceImp" class="com.tiros.caredog.cd.serviceimp.ReportServiceImp"> <property name="reportDao" ref="reportDaoImp"></property> </bean>