Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-WS :: ways to call a web service from a standalone Java 7 SE client

Tags:

java

jax-ws

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?

like image 858
Marcus Junius Brutus Avatar asked Oct 02 '22 21:10

Marcus Junius Brutus


1 Answers

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 not retrieved 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 not hardcoded in the stub.
like image 163
Marcus Junius Brutus Avatar answered Oct 07 '22 19:10

Marcus Junius Brutus