使用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注释的方式
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
);
二,通过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
]);
}
}
三,使用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,可以指定如下两种不同的方式:
(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
"
);
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
其结果与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
转自:http://www.blogjava.net/fastzch/archive/2008/08/28/225439.html
备注:
如果是采取传递对象的形式,则客户端用到的bean不仅内容要和服务器上的相同并且包名都必须是一样的!
常遇到的异常解决办法
1.java.io.FileNotFoundException: class path resource [org/codehaus/xfire/spring/xfire.xml] cannot be opened because it does not exist
没有找到org/codehaus/xfire/spring/xfire.xml。查看你的xfire所用的jar中是否包括了org/codehaus/xfire/spring/xfire.xml文件
2.java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xfire-servlet.xml]
查看项目的webroot下面是否有/WEB-INF/xfire-servlet.xml文件
3.org.codehaus.xfire.XFireRuntimeException: Cannot create mapping for java.util.List, unspecified component type for return type of method findAllUser in interface com.devil13th.service.IUserService
发布的接口一定是有List或collection等返回类型,而且没有配置这些返回类型的.xml文件或是没有配置泛型的类型。查看【服务接口相关的xml】部分中的教程内容
plus:xfire client http authentic
Client basicAuth = ((XFireProxy) Proxy.getInvocationHandler(service))
.getClient();
basicAuth.setProperty(Channel.USERNAME, "user");
basicAuth.setProperty(Channel.PASSWORD, "pwd");
分享到:
相关推荐
Xfire客户端调用webservice接口所需 jar包,亲测可用。
cxf_axis_xfire客户端调用的几种方法[收集].pdf
cxf_axis_xfire客户端调用的几种方法.docx
webservice ixfir 客户端端开发 对象传递使webservice开发更加简单快捷
xfire客户端生成自动生成java代码调用
此程序demo详细描述的从发布接口到调用接口的全部过程,详细描述了Xfire调用webservice参数无法传递的问题。
xfire开发webservice与调用 用起来比axis简单,容易上手。没有axis那么复杂。性能也比axis好。附件里面包含教程文档及实例代码
NULL 博文链接:https://fenghaoo.iteye.com/blog/434000
xfire客户端jar包以及一个Demo样板 Demo样板不能直接使用,因为和每个人调用的wsdl直接相关。 请修改后使用!
NULL 博文链接:https://zhangzhaoaaa.iteye.com/blog/1718819
xfire,服务端和客户端实现 xfire调用webservice
webservice服务,使用Xfire框架调用服务的常见客户端代码
xfire-core-1.2.6.jar,xfire客户端调用所需jar包
xfire客户端调用所需jar,找的时候很辛苦
XFire 1.2.6调用Web Service的几种方法
axis,jax-ws,xfire客户端调用分析,以及webservice万能客户端
提供客户端调用xfire开发的webservice接口详细过程;同时提供调用xfire依赖的jar包。适合新手~
NULL 博文链接:https://gyl892014311.iteye.com/blog/2201082
webservice客户端几种方式调用汇总,客户端代码生成及调用方式详细说明等
xfire实现的实例和远程调用的客户端代码