本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系(
fastzch@163.com
)。
未经许可的转载,本人保留一切法律权益。
一直以来,发现有某些人完全不尊重我的劳动成果,随意转载,提醒一下那些人小心哪天惹上官司。
虽然XFire现在已经被CXF取代,但在现在已经开发的项目中,还是有不少使用XFire来实现Web Service的。前段时间,我写的使用XFire开发Web Service服务端的文章《 XFire完整入门教程》受到了大家的好评,给大家带来了方便,很是高兴。今天来谈谈用XFire开发客户端。
本文也将继续使用上一篇文章的服务端来做为服务端。使用XFire开发Web Service客户端分为如下两大类:
一、服务提供者告诉你interface,你可以使用如下三种方式来开发:
YourService即是服务提供者告诉给你的一个interface(当然,也可以根据WSDL的定义,自己定义一个同样的interface)。
1,简单的方式
二,通过WSDL创建一个动态的客户端,如下:
三,使用ANT工具或命令行通过WSDL生成一个客户端:
1,使用ANT生成客户端,ANT脚本如下:
(1)jaxb(Java Architecture for XML Binding, https://jaxb.dev.java.net/):使用此种方式时,会自动生成更多的Request和Resopnse类。
(2)xmlbeans
调用方式如下:
2,使用命令生成客户端的命令如下:
未经许可的转载,本人保留一切法律权益。
一直以来,发现有某些人完全不尊重我的劳动成果,随意转载,提醒一下那些人小心哪天惹上官司。
虽然XFire现在已经被CXF取代,但在现在已经开发的项目中,还是有不少使用XFire来实现Web Service的。前段时间,我写的使用XFire开发Web Service服务端的文章《 XFire完整入门教程》受到了大家的好评,给大家带来了方便,很是高兴。今天来谈谈用XFire开发客户端。
本文也将继续使用上一篇文章的服务端来做为服务端。使用XFire开发Web Service客户端分为如下两大类:
一、服务提供者告诉你interface,你可以使用如下三种方式来开发:
YourService即是服务提供者告诉给你的一个interface(当然,也可以根据WSDL的定义,自己定义一个同样的interface)。
1,简单的方式
Service serviceModel
=
new
ObjectServiceFactory().create(YourService.
class
);
YourService service = (YourService)
new XFireProxyFactory().create(serviceModel, " http://your/remote/url " );
2,JSR 181注释的方式
YourService service = (YourService)
new XFireProxyFactory().create(serviceModel, " http://your/remote/url " );
Service serviceModel
=
new
AnnotationServiceFactory().create(YourService.
class
);
YourService client = (YourService)
new XFireProxyFactory().create(serviceModel, " http://your/remote/url " );
3,混合方式
YourService client = (YourService)
new XFireProxyFactory().create(serviceModel, " http://your/remote/url " );
Service serviceModel
=
new AnnotationServiceFactory(
new Jsr181WebAnnotations(),
XFireFactory.newInstance().getXFire().getTransportManager(),
new AegisBindingProvider( new JaxbTypeRegistry())).create(YourService. class );
new AnnotationServiceFactory(
new Jsr181WebAnnotations(),
XFireFactory.newInstance().getXFire().getTransportManager(),
new AegisBindingProvider( new JaxbTypeRegistry())).create(YourService. class );
二,通过WSDL创建一个动态的客户端,如下:
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]);
}
}
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工具或命令行通过WSDL生成一个客户端:
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 >
请注意,脚本中有一个参数binding,可以指定如下两种不同的方式:
< 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 >
(1)jaxb(Java Architecture for XML Binding, https://jaxb.dev.java.net/):使用此种方式时,会自动生成更多的Request和Resopnse类。
(2)xmlbeans
调用方式如下:
AbcServiceClient client
=
new
AbcServiceClient();
String url = " http://localhost:8080/xfireTest/services/TestService " ;
String result = client.getAbcPort(url).sayHello( " Robin " );
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
: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
其结果与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