您的位置:首页 > 编程语言 > Java开发

采用XFire调用Java Web Service

2014-08-13 22:41 501 查看
客户端调用Webservice的两种方式

一、通过服务端提供的接口类进行调用。

代码如下:

view source

package
com.xfire.client;
import
java.net.MalformedURLException;
import
java.util.List;
import
org.codehaus.xfire.XFire;
import
org.codehaus.xfire.XFireFactory;
import
org.codehaus.xfire.client.XFireProxyFactory;
import
org.codehaus.xfire.service.Service;
import
org.codehaus.xfire.service.binding.ObjectServiceFactory;
import
com.xfire.domain.Person;
import
com.xfire.spring.IPersonService;
public
class
PojoInvokeClient {
public
static

void
main(String[] args) {
Service serviceModel =
new

ObjectServiceFactory().create(IPersonService.
class
);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory =
new

XFireProxyFactory(xfire);
String serviceUrl =
"http://127.0.0.1:8080/xfire/services/PersonService"
;
IPersonService client =
null
;
try
{
client = (IPersonService) factory.create(serviceModel, serviceUrl);
}
catch

(MalformedURLException e) {
System.out.println(
"Client call webservice has exception: "
+ e.toString());
}
String result1 =client.sayHello(
"张三"
);
}
}
二、直接通过url调用, 不用客户端提供接口类。

view source

package
com.xfire.client;
import
java.net.MalformedURLException;
import
java.net.URL;
import
org.codehaus.xfire.client.Client;
public
class
UrlInvokeClient {
public
static

void
main(String[] args) {
Client client =
null
;
try
{
client =
new

Client(
new
URL(
"http://127.0.0.1:8080/xfire/PersonService.ws?wsdl"
));
Object[] result1 = client.invoke(
"sayHello"
,
new

Object[] {
"张三"
});
System.out.println(result1[
0
]);
}

catch
(MalformedURLException e) {
e.printStackTrace();
}

catch
(Exception e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: