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.
For the client side I've found the following three ways that seem to work (following use of wsimport
to generate the client stubs):
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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With