JAVA使用XFire开发Web Service客户端几种调用方式

一、服务提供者告诉你interface,你可以使用如下三种方式来开发: 
YourService即是服务提供者告诉给你的一个interface(当然,也可以根据WSDL的定义,自己定义一个同样的interface)。 

1、简单的方式

1 Service serviceModel = new ObjectServiceFactory().create(YourService.class);
2 YourService service = (YourService)new XFireProxyFactory().create(serviceModel, "http://your/remote/url");

2、JSR 181注释的方式

1 Service serviceModel = new AnnotationServiceFactory().create(YourService.class);
2 YourService client = (YourService)new XFireProxyFactory().create(serviceModel, "http://your/remote/url");

3、混合方式

1 Service serviceModel = new AnnotationServiceFactory(new Jsr181WebAnnotations(), XFireFactory.newInstance().getXFire().getTransportManager(), new AegisBindingProvider(new JaxbTypeRegistry())).create(YourService.class);


二、通过WSDL创建一个动态的客户端,如下:

01 import java.net.MalformedURLException;
02 import java.net.URL;
03  
04 import org.codehaus.xfire.client.Client;
05  
06 public class DynamicClientTest {
07     public static void main(String[] args) throws MalformedURLException, Exception {
08         Client client = new Client(new URL("http://localhost:8080/xfiretest/services/TestService?wsdl"));
09         Object[] results = client.invoke("sayHello"new Object[] { "Firends" });
10         System.out.println(results[0]);
11     }
12 }

 

三,使用ANT工具或命令行通过WSDL生成一个客户端:
1、使用ANT生成客户端,ANT脚本如下:

01 <?xml version="1.0"?>
02 <project name="wsgen" default="wsgen" basedir=".">
03     <path id="classpathId">
04         <fileset dir="./WebRoot/WEB-INF/lib">
05             <include name="*.jar" />
06         </fileset>
07     </path>
08     <taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask">
09     </taskdef>
10     <target name="wsgen" description="generate client">
11         <wsgen outputDirectory="./src/" wsdl="abc.wsdl" binding="xmlbeans" package="com.abc.p" overwrite="true" />
12     </target>
13 </project>

请注意,脚本中有一个参数binding,可以指定如下两种不同的方式:
(1)jaxb(Java Architecture for XML Binding,https://jaxb.dev.java.net/):使用此种方式时,会自动生成更多的Request和Resopnse类。
(2)xmlbeans

调用方式如下:

1 AbcServiceClient client = new AbcServiceClient();
2 String url = "http://localhost:8080/xfireTest/services/TestService";
3 String result = client.getAbcPort(url).sayHello("Robin");

2、使用命令生成客户端的命令如下:

1 gpath=xfire-all-1.2-SNAPSHOT.jar:ant-1.6.5.jar:jaxb-api-2.0EA3.jar:stax-api-1.0.1.jar:jdom-1.0.jar:jaxb-impl-2.0EA3.jar\
2 :jaxb-xjc-2.0-ea3.jar:wstx-asl-2.9.3.jar:commons-logging-1.0.4.jar:activation-1.1.jar:wsdl4j-1.5.2.jar:XmlSchema-1.0.3.jar:xfire-jsr181-api-1.0-M1.jar;
3  
4 java -cp $gpath org.codehaus.xfire.gen.WsGen -wsdl http://localhost:8080/xfire/services/Bookservice?wsdl -o . -p pl.tomeks.client -overwrite true

其结果与ANT生成的一样。


四、参考资源:
1、XFire 1.2.6手册(http://xfire.codehaus.org/User%27s+Guide)
2、http://xfire.codehaus.org/Client+API
3、http://xfire.codehaus.org/Dynamic+Client

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值