雖然XFire現(xiàn)在已經(jīng)被CXF取代,但在現(xiàn)在已經(jīng)開發(fā)的項(xiàng)目中,還是有不少使用XFire來(lái)實(shí)現(xiàn)Web Service的。前段時(shí)間,我寫的使用XFire開發(fā)Web Service服務(wù)端的文章《
XFire完整入門教程》受到了大家的好評(píng),給大家?guī)?lái)了方便,很是高興。今天來(lái)談?wù)動(dòng)肵Fire開發(fā)客戶端。
本文也將繼續(xù)使用上一篇文章的服務(wù)端來(lái)做為服務(wù)端。使用XFire開發(fā)Web Service客戶端分為如下兩大類:
一、服務(wù)提供者告訴你interface,你可以使用如下三種方式來(lái)開發(fā):
YourService即是服務(wù)提供者告訴給你的一個(gè)interface(當(dāng)然,也可以根據(jù)WSDL的定義,自己定義一個(gè)同樣的interface)。
1,簡(jiǎn)單的方式
Service serviceModel = new ObjectServiceFactory().create(YourService.class);
YourService service = (YourService)
new XFireProxyFactory().create(serviceModel, "http://your/remote/url"); 2,JSR 181注釋的方式
Service serviceModel = new AnnotationServiceFactory().create(YourService.class);
YourService client = (YourService)
new XFireProxyFactory().create(serviceModel, "http://your/remote/url"); 3,混合方式
Service serviceModel =
new AnnotationServiceFactory(
new Jsr181WebAnnotations(),
XFireFactory.newInstance().getXFire().getTransportManager(),
new AegisBindingProvider(new JaxbTypeRegistry())).create(YourService.class); 二,通過(guò)WSDL創(chuàng)建一個(gè)動(dòng)態(tài)的客戶端,如下:
package test;
import java.net.MalformedURLException;
import java.net.URL;
import org.codehaus.xfire.client.Client;
public class DynamicClientTest {
public static void main(String[] args) throws MalformedURLException,
Exception {
Client client = new Client(new URL(
"http://localhost:8080/xfiretest/services/TestService?wsdl"));
Object[] results = client
.invoke("sayHello", new Object[] { "Firends" });
System.out.println(results[0]);
}
}
三,使用ANT工具或命令行通過(guò)WSDL生成一個(gè)客戶端:
1,使用ANT生成客戶端,ANT腳本如下:
<?xml version="1.0"?>
<project name="wsgen" default="wsgen" basedir=".">
<path id="classpathId">
<fileset dir="./WebRoot/WEB-INF/lib">
<include name="*.jar" />
</fileset>
</path>
<taskdef classpathref="classpathId" name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask">
</taskdef>
<target name="wsgen" description="generate client">
<wsgen outputDirectory="./src/" wsdl="abc.wsdl" binding="xmlbeans" package="com.abc.p" overwrite="true" />
</target>
</project> 請(qǐng)注意,腳本中有一個(gè)參數(shù)binding,可以指定如下兩種不同的方式:
(1)jaxb(Java Architecture for XML Binding,
https://jaxb.dev.java.net/):使用此種方式時(shí),會(huì)自動(dòng)生成更多的Request和Resopnse類。
(2)xmlbeans
調(diào)用方式如下:
AbcServiceClient client = new AbcServiceClient();
String url = "http://localhost:8080/xfireTest/services/TestService";
String result = client.getAbcPort(url).sayHello("Robin"); 2,使用命令生成客戶端的命令如下:
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\
: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;
java -cp $gpath org.codehaus.xfire.gen.WsGen -wsdl http://localhost:8080/xfire/services/Bookservice?wsdl -o . -p pl.tomeks.client -overwrite true 其結(jié)果與ANT生成的一樣。
四,參考資源:
1,XFire 1.2.6手冊(cè)(http://xfire.codehaus.org/User%27s+Guide)
2,http://xfire.codehaus.org/Client+API
3,http://xfire.codehaus.org/Dynamic+Client
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。