JAX-WS :: ways to call a web service from a standa

2019-03-14 07:38发布

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?

标签: java jax-ws
1条回答
相关推荐>>
2楼-- · 2019-03-14 08:10

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.
查看更多
登录 后发表回答