java 调用 web service

本文展示了如何使用Java客户端调用Web服务,通过引入必要的包,设置URL、命名空间和服务名,创建并配置Service实例,进而实现对Web服务接口的动态代理调用。
Invoking web services with Java clients
[url]http://www.ibm.com/developerworks/webservices/library/ws-javaclient/index.html#listing4[/url]



/*
* DynamicProxy class
* J2SE unamanaged client
* Service lookup: JAX-RPC ServiceFactory
* Service access: Dynamic Proxy
*/

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;

public class DynamicProxy {

public interface HelloInterface {

char[] getGreeting(String string);

}

public static void main(String[] args) {
String wsdlURL = "http://localhost:6080/Sample2WebService/services/Sample2?wsdl";
String namespace = "http://Sample2.wsdk.ibm.com";
String serviceName = "HelloInterfaceService";
String portName = "Sample2";

try {

/* Service lookup */
ServiceFactory serviceFactory = ServiceFactory.newInstance();
Service sampleService = serviceFactory.createService(new URL(
wsdlURL), new QName(namespace, serviceName));

/* Service access */
HelloInterface myProxy = (HelloInterface) sampleService.getPort(
new QName(namespace, portName), HelloInterface.class);

/* Service invocation */
System.out.println(myProxy.getGreeting(args[0]));

} catch (Exception e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值