这几天在弄WebService的东西,是使用XFire+spring的。走了不少的弯路,差点想放弃了。为了日后方便,先记下这几天的收获。

刚开始的时候,不知道xfire还有针对spring的版本,直接将pojo expose成为web service。最后发现竟然不能够使用spring的注入功能。换句话说,即如果这个pojo使用了spring的注入机制,注入了其他bean的话,无法注入。

在看了网上无数篇copy的文章后,发现需要另外整合XFire+spring。先整理如下:

1. 制作web service的service类。这里需要同时具有接口和实现类。

我这里假设有一个ExCommService和他的接口IExCommService。ExCommService中是有使用spring注入了属性的。并通过spring配置其为一个Service,名字为exCommService。

2. 通过myeclipse增加XFire的code library和HTTP Client library。

3. 修改web配置,令符合http://{ip}:{port}/{prjName}/services/*的url才作为web service的方法暴露给用户。

3.1 增加DispatcherServlet。

因为spring与xfire的整合是通过DispatcherServlet完成的,所以必须要配置。

<servlet-name>dipatcher</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
       <param-name>contextConfigLocation</param-name>      <!-- 我的配置文件都是以config_开头的 -->
       <param-value>/WEB-INF/config_*.xml</param-value>
     </init-param>
     <load-on-startup>0</load-on-startup>
   </servlet>

3.2 配置mapping

<servlet-mapping>
     <servlet-name>dipatcher</servlet-name>
     <url-pattern>*.do</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
     <servlet-name>dipatcher</servlet-name>
     <url-pattern>/services/*</url-pattern>
   </servlet-mapping>

4. 配置web service对应的配置文件,config_webservice.xml

这个文件一般需要自己来建立。因为你肯定也不希望把所有的配置都放在dipatcher-servlet.xml中吧?

4.1 为spring mvc和xfire做配置设定

这点很重要,没有这句话,在尝试看wsdl文件时会报错:“No adapter for handler [....XFireExporter@60e78]: Does your handler implement a supported interface like Controller? ”

<bean id="controllerHandlerAdapter" class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

4.2 加载xfire为spring定制的一些bean

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

4.3 定义一个web service的父bean,这样可以节省配置。这些都是死配置,不解释了。

<bean id="baseWebService"
         class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">
         <property name="serviceFactory" ref="xfire.serviceFactory" />
         <property name="xfire" ref="xfire" />
     </bean>

4.4 定义web service的相关信息

<bean id="exCommService.xfire" parent="baseWebService">
         <property name="serviceBean" ref="exCommService" />
         <property name="serviceClass" value="com.wtmec.eccn.ws.IExCommService" />
         <property name="namespace" value="http://10.4.1.16:8080/eccn" />
     </bean>

其中serviceBean属性代表需要作为web service的bean在spring中定义的名称。参考第1点;

serviceClass属性代表serviceBean所实现的接口的完整路径。

namespace属性代表的是将来service端应该通过哪个链接访问我们的web service。

5. 配置HandlerMapping。spring需要这个配置来将用户的请求转向我们的service Bean中去。

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
         <property name="urlMap">
             <map>
                 <entry key="/exCommService"><ref bean="exCommService.xfire"/></entry>
             </map>
         </property>
     </bean>

经过上面的配置后,启动tomcat,然后通过:http://10.4.1.16:8080/eccn/services/exCommService?wsdl就可以访问wsdl文件了。

 

下面来讲客户端调用。===================

1. 加入跟server端一样的library。参考上面的第1点

2. 将serviceClass对应的接口加入客户端中。

3. 书写调用代码: PS:代码太简单,不另外注释了。

private String url = "http://10.4.1.16:8080/eccn/services/exCommService";
     public String checkLicenseByOB(String obno) throws Exception{
         Service serviceModel = new ObjectServiceFactory().create(IExCommService.class);
         IExCommService service = (IExCommService)
         new XFireProxyFactory().create(serviceModel, url);
         
         return service.checkLicenseByOB(obno);
     }

在生成接口对应的service后,用户就可以像调用本地方法一样调用远程的方法了。

 

最后要感谢一篇国外的帖子。他解决了我的问题

http://forum.springsource.org/archive/index.php/t-26378.html