JAX-WS :: 从独立 Java 7 SE 客户端调用 Web 服务的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18925888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAX-WS :: ways to call a web service from a standalone Java 7 SE client
提问by Marcus Junius Brutus
I am experimenting with standalone JAX-WS web services, server and client side (meaning, not running inside a Java EE container). A good SO post showing standalone server-side is this one.
我正在试验独立的 JAX-WS Web 服务、服务器端和客户端(意思是,不在 Java EE 容器内运行)。一个很好的 SO 帖子显示了独立的服务器端是这个。
For the client side I've found the following three ways that seem to work (following use of wsimport
to generate the client stubs):
对于客户端,我发现以下三种似乎有效的方法(使用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"));
}
}
I am not aware of any other patterns of client-side access or how the ways shown above compare against each other.
我不知道客户端访问的任何其他模式或上面显示的方式如何相互比较。
Any other methods or trade-offs one should be aware of?
应该注意任何其他方法或权衡?
采纳答案by Marcus Junius Brutus
In the end, after some experimentation, I think the way shown below (taken from here) has distinct advantages compared to the previous three in my question:
最后,经过一些实验,我认为下面显示的方式(取自此处)与我问题中的前三种方式相比具有明显的优势:
{ // WAY 4
QName qname = new QName("urn:playground:jax-ws", "MyService");
MyService service = new MyService(null, qname);
IHello port = service.getHelloPort();
BindingProvider bindingProvider = (BindingProvider) port;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
System.out.println(port.sayHello("John Silver"));
}
The advantages being that:
优点在于:
- the WSDL is notretrieved at runtime (and why should it be? it's already been used at code creation time to create the client stub)
- the URL of the service is nothardcoded in the stub.
- 在运行时不检索WSDL (为什么要检索?它已经在代码创建时用于创建客户端存根)
- 服务的 URL没有硬编码在存根中。