xfire+spring整合webservice

shuhuai007發表於2011-12-20
伺服器端: 

1、建立一個新的專案springXfireWebService (客戶端) 

2、在官網http://xfire.codehaus.org/Download中下載:xfire-distribution-1.2.6.zip (在建立專案後將所有jar包放到lib包中,測試刪掉幾個發現不行,似乎都是需要的,忘高手指明不足)和xfire-all-1.2.6.jar 。 

3、建立webService: 

Java程式碼  收藏程式碼
  1. package com.ebiz.xfire.service;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.ebiz.xfire.domain.CscProject;  
  6.   
  7. /** 
  8.  * @author Ethan,Woo 
  9.  */  
  10. public interface CscWebService {  
  11.   
  12.     /** 
  13.      *@author Ethan,Woo 
  14.      *@desc "載入專案庫資料" 
  15.      *@date 2009-09-28 
  16.      */  
  17.     List<CscProject> getCscProjectList(CscProject t);  
  18.   
  19.       
  20. }  


4、實現: 

Java程式碼  收藏程式碼
  1. package com.ebiz.xfire.service.impl;  
  2.   
  3. import com.ebiz.xfire.service.WyWebService;  
  4.   
  5. /** 
  6.  * @author Ethan,Woo 
  7.  */  
  8. public class WyWebServiceImpl implements WyWebService {  
  9.   
  10.     public void printHello() {  
  11.         System.out.println("Hello!");  
  12.     }  
  13.   
  14. }  


5、引入jar包:xfire-distribution-1.2.6.zip 裡面的包、xfire-all-1.2.6.jar和spring-2.5.6.jar。 

6、與spring整合spring-context.xml: 

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  7.                 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  8.                 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd  
  9.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  
  10.     default-autowire="byName">  
  11.   
  12.     <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />  
  13.   
  14.     <!-- ====================testSpringXfire================= -->  
  15.     <bean id="wyWebServiceImpl" class="com.ebiz.xfire.service.impl.WyWebServiceImpl" />  
  16.     <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" />  
  17.     <bean name="webService" class="org.codehaus.xfire.spring.ServiceBean">  
  18.         <property name="serviceBean" ref="wyWebServiceImpl"></property>  
  19.         <property name="serviceClass" value="com.ebiz.xfire.service.WyWebService"></property>  
  20.         <property name="inHandlers">  
  21.             <list>  
  22.                 <ref bean="addressingHandler" />  
  23.             </list>  
  24.         </property>  
  25.     </bean>  
  26.   
  27. </beans>  


7、配置web.xml: 

Xml程式碼  收藏程式碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  4.     <display-name>XFireTest</display-name>  
  5.     <welcome-file-list>  
  6.         <welcome-file>index.html</welcome-file>  
  7.         <welcome-file>index.jsp</welcome-file>  
  8.     </welcome-file-list>  
  9.   <context-param>   
  10.     <param-name>contextConfigLocation</param-name>   
  11.     <param-value>classpath:spring-context.xml</param-value>   
  12.   </context-param>   
  13.   <listener>   
  14.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  15.   </listener>   
  16.   <servlet>   
  17.     <servlet-name>XFireServlet</servlet-name>   
  18.     <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>   
  19.   </servlet>   
  20.   <servlet-mapping>   
  21.     <servlet-name>XFireServlet</servlet-name>   
  22.     <url-pattern>/servlet/XFireServlet/*</url-pattern>   
  23.   </servlet-mapping>   
  24.   <servlet-mapping>   
  25.     <servlet-name>XFireServlet</servlet-name>   
  26.     <url-pattern>/services/*</url-pattern>   
  27.   </servlet-mapping>   
  28. </web-app>  


8、啟動專案springXfireWebService (客戶端),沒有出錯 第一步OK啦!! 

客戶端: 

1、建立新的專案springXfireClient(客戶端) 

2、建立service: 

Java程式碼  收藏程式碼
  1. package com.ebiz.xfire.service;  
  2.   
  3.   
  4. /** 
  5.  * @author Ethan,woo 
  6.  * @version 2009-09-28 
  7.  */  
  8. public interface WyWebService {  
  9.   
  10.     /** 
  11.      *@desc "列印Hello" 
  12.      */  
  13.      void printHello();  
  14.   
  15. }  


Java程式碼  收藏程式碼
  1. 注意:這裡的包的目錄結構一定要和客戶端的包的結構一定要一樣(本人吃過虧的~~~~(>_<)~~~~ )  


3、建立test.java 

Java程式碼  收藏程式碼
  1. package com.testxfire.client;  
  2.   
  3. import java.net.MalformedURLException;  
  4.   
  5. import org.codehaus.xfire.XFireFactory;  
  6. import org.codehaus.xfire.client.XFireProxyFactory;  
  7. import org.codehaus.xfire.service.Service;  
  8. import org.codehaus.xfire.service.binding.ObjectServiceFactory;  
  9.   
  10. import com.ebiz.xfire.service.WyWebService;  
  11.   
  12. public class Test {  
  13.   
  14.     public static void main(String[] args) {  
  15.   
  16.         Service serviceModel = new ObjectServiceFactory().create(WyWebService.class);  
  17.   
  18.         XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire());  
  19.   
  20.         String url = "http://localhost:8080/springXfireWebService/services/WyWebService";  
  21.         try {  
  22.             WyWebService cws = (WyWebService) factory.create(serviceModel, url);  
  23.             cws.printHello();  
  24.   
  25.         } catch (MalformedURLException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.   
  29.     }  
  30.   
  31. }  


4、在控制檯出現: 

Hello 

完成。注意這裡沒有涉及到與資料庫的互動,可以在實現中完善,另外在在spirng中配置。

相關文章