1、在eclipse下新建一個web工程,名爲:xfireDemo: html
2、導入XFire用戶庫。該庫中應包含xfire-1.26目錄下的xfire-all-1.2.6.jar文件,以及xfire-1.2.6\lib目錄下的全部文件。 java
3、修改項目下的web.xml文件爲: web
<?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>xfireDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>xfireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
web.xml中添加的servlet映射代表,全部匹配「/services/*」的url請求所有交給org.codehaus.xfire.transport.http.XFireConfigurableServlet來處理。 spring
4、編寫須要發佈爲WebService的Java接口: apache
package com.yao.xfire.service; public interface MathService { public int add(int a,int b); }5、編寫實現其接口的類:
package com.yao.xfire.service; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class MathServiceImpl implements MathService { private final Log log = LogFactory.getLog(MathServiceImpl.class); public int add(int a, int b) { log.info("invoke method add."); return a + b; } }6、在WebContent\META-INF目錄下新建xfire文件夾,而後在xfire目錄下添加一個XFire使用的配置文件services.xml,該配置文件中的內容反映了要將哪些java類發佈爲web服務。本例中的services.xml內容以下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>MathService</name> <namespace>http://com.yao.xfire.service/MathService</namespace> <serviceClass>com.yao.xfire.service.MathService</serviceClass> <implementationClass>com.yao.xfire.service.MathServiceImpl</implementationClass> </service> </beans>
XFire會藉助Spring來解析services.xml,從中提取須要發佈爲WebService的配置信息。
不少文章介紹到這裏就完了,然而當我按照他們所說的啓動WebService ,而後經過http://localhost:8080/XFireZhuweiTest/services/MathService?wsdl 來訪問服務描述時,卻拋出了異常,說services.xml文件不存在--
「org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/xfire/services.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/xfire/services.xml] cannot be opened because it does not exist」。 app
7、很是關鍵的一點,在WebContent\WEB-INF目錄下新建classes文件夾,而後須要將WebContent下的整個META-INF文件夾剪切到新建的classes文件夾下。
到這裏,項目的完整目錄結構以下: eclipse
8、右鍵->Run As ->Run On Server,關聯到你機器上的Tomcat,而後會啓動Tomcat,以啓動web服務。 jsp
9、在IE中輸入 http://localhost:8080/xfireDemo/services/MathService?wsdl 會獲得正確的web服務描述文檔。 url
10、新建一個客戶端訪問類MathServiceClient: spa
package com.yao.xfire.client; import java.net.MalformedURLException; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.Client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import org.codehaus.xfire.transport.http.CommonsHttpMessageSender; import com.yao.xfire.service.MathService; public class MathServiceClient { private static MathService mathService; private MathServiceClient(){} public static MathService getMathService(String serviceUrl) throws MalformedURLException{ if(mathService == null){ //建立服務 Service srvcModel = new ObjectServiceFactory().create(MathService.class); //建立XFire對象 XFireProxyFactory factory = new XFireProxyFactory(XFireFactory.newInstance().getXFire()); //調用Web服務 mathService = (MathService) factory.create(srvcModel, serviceUrl); //設置客戶端調用的屬性 Client client = Client.getInstance(mathService); client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "300"); client.setProperty(CommonsHttpMessageSender.DISABLE_KEEP_ALIVE, "true"); client.setProperty(CommonsHttpMessageSender.DISABLE_EXPECT_CONTINUE, "true"); //若是須要設置代理 //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_HOST, "10.3.1.6" ); //client.setProperty(CommonsHttpMessageSender.HTTP_PROXY_PORT, "8080"); } return mathService; } public static void main(String[] args) throws MalformedURLException{ MathService service = MathServiceClient.getMathService("http://localhost:8080/xfireDemo/services/MathService"); int result = service.add(1, 14); System.out.println("get the result: " + result); } }將獲得結果:get the result: 15。