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

1

5

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?

Entry answered 20/9, 2013 at 21:7 Comment(2)
I used way 1 to test web service access from console applications apart from using a Web Service client tool like Eclipse Web Service Explorer. Apart from these, I think you can do the work parsing the WSDL file manually and more boilerplate code, I've seen an example of this in JDeveloper 10, but it is not worth the time to even check it out.Dexterdexterity
Where qname parameters are defined?Cento
E
4

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.
Entry answered 8/10, 2013 at 15:28 Comment(1)
It would be nice if we dont have to use service class, some sort of generic will help to make more generic code.Delegation

© 2022 - 2024 — McMap. All rights reserved.