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 ,不然会报错。