Xfire相對Axis2 我的感受相對簡單一點,由於myEclipse上能夠自動生成。並且在目前來說,Xfire也是更受歡迎一點。java
首先說明Xfire所用jar包:http://download.csdn.net/detail/u014104269/9028679web
下面是步驟:app
步驟一:打開myEclipse ,New-》web Service projectspa
寫上name ,選擇Xfire ,就直接Finish 就行了。.net
而後在項目上,NEw-》other-》web Service code
Finishxml
此時打開services.xml 發現其中多了些東西:blog
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>MyService</name> <serviceClass>com.demo.service.IMyService</serviceClass> <implementationClass> com.demo.service.MyServiceImpl </implementationClass> <style>wrapped</style> <use>literal</use> <scope>application</scope> </service></beans>
多了兩個文件 一個是接口文件:接口
package com.demo.service; //Generated by MyEclipse public interface IMyService { public String example(String message); }
另外一個是對該接口的實現:ip
package com.demo.service; //Generated by MyEclipse public class MyServiceImpl implements IMyService { public String example(String message) { return message; } }
此時:
使用client代碼就能夠調用了:
package com.demo.client; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import com.demo.service.IMyService; public class Test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String serviceUrl = "http://localhost:8081/Demo/services/MyService"; Service serviceModel = new ObjectServiceFactory().create(IMyService.class, null, "http://localhost:8081/Demo/services/MyService?wsdl", null); XFireProxyFactory serviceFactory = new XFireProxyFactory(); try{ IMyService service = (IMyService)serviceFactory.create(serviceModel,serviceUrl); String hello = service.example("cys"); System.out.println(hello); }catch(Exception e){ e.printStackTrace(); } } }
另外一種方法是 使用JDK中lib文件夾下的一個叫作 wsimport.exe的文件
把在該文件夾下生成的文件所有導入到項目中 就能夠了:
客戶端代碼是:
package com.demo.client; public class T { /** * @param args */ public static void main(String[] args) { MyService service =new MyService(); MyServicePortType spt = service.getMyServiceHttpPort(); System.out.println(spt.example("haahahha")); } }
不過在使用第二種方法時候,須要刪除jar包中的xfire-all ,不然會報錯。