Web Service的三种调用方式

下面列举三种调用WEB SERVICE的方式,仅供参考:

[b]方式一[/b]:spring + XFireClientFactoryBean的方式
首先,配置XFireClientFactoryBean:

<bean id="helloService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<!-- 服务接口类 -->
<property name="serviceClass">
<value>com.mzh.server.IHelloService</value>
</property>
<!-- wsdlDocumentUrl -->
<property name="wsdlDocumentUrl">
<value>http://192.168.9.97:8089/AlarmCollector/services/HelloService?wsdl</value>
</property>
</bean>

其次,调用代码如下:

/**
* xfire+spring调用
*/
public static void invoke1(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloService helloService=(IHelloService)ctx.getBean("helloService");
String helloMsg = helloService.sayHello("mazh");
System.out.println(helloMsg);
}

[b]方式二[/b]:利用org.codehaus.xfire.client.Client实现调用

/**
* org.codehaus.xfire.client.Client客户端调用
*/
public static void invoke2(){
try {
Client client = new Client(new URL("http://192.168.9.97:8089/AlarmCollector/services/HelloService?wsdl"));
Object[] results = client.invoke("sayHello", new Object[]{"zyd"});
System.out.println(results[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

[b]方式三[/b]:利用axis.client实现调用

/**
* axis客户端调用
* @throws ServiceException
* @throws MalformedURLException
* @throws RemoteException
*/
public static void invovke3() throws ServiceException, MalformedURLException, RemoteException {
String endpoint = "http://localhost:8089/AlarmCollector/services/HelloService";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setOperationName("sayHello");
call.addParameter("name",
org.apache.axis.encoding.XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.SOAP_STRING);
System.out.println(call.invoke(new Object[]{"mzh"}));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值