【发布时间】:2013-09-20 21:07:22
【问题描述】:
我正在试验独立的 JAX-WS Web 服务、服务器端和客户端(意思是,不在 Java EE 容器内运行)。一个很好的显示独立服务器端的 SO 帖子是 this one。
对于客户端,我发现以下三种似乎可行的方法(使用wsimport 生成客户端存根):
public static void main(String[] args) throws Exception {
String serviceURL = "http://localhost:9000/soap?wsdl";
{ // WAY 1
URL url = new URL(serviceURL);
QName qname = new QName("urn:playground:jax-ws", "MyService");
Service service = Service.create(url, qname);
IHello port = service.getPort(IHello.class);
System.out.println(port.sayHello("Long John"));
}
{ // WAY 2
MyService service = new MyService();
IHello port = service.getHelloPort();
((javax.xml.ws.BindingProvider) port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
System.out.println(port.sayHello("Long John"));
}
{ // WAY 3
URL url = new URL(serviceURL);
QName qname = new QName("urn:playground:jax-ws", "MyService");
MyService service = new MyService(url, qname);
IHello port = service.getHelloPort();
System.out.println(port.sayHello("Long John"));
}
}
我不知道客户端访问的任何其他模式或上面显示的方式如何相互比较。
任何其他的方法或权衡是人们应该知道的?
【问题讨论】:
-
我使用 方式 1 来测试控制台应用程序对 Web 服务的访问,除了使用像 Eclipse Web Service Explorer 这样的 Web 服务客户端工具。除此之外,我认为您可以手动解析 WSDL 文件和更多样板代码,我在 JDeveloper 10 中看到过这样的示例,但不值得花时间去检查。
-
qname参数在哪里定义?