users@jax-ws.java.net

How to write a client with JAX-WS final release

From: Lukasz Chomin <lukaszc_at_computec.com.pl>
Date: Sun, 21 May 2006 12:05:03 +0200

Hi,
I try to write a simple client which consume a web service. I don't want to
use any ant tasks delivered with Sun Java Application Server 9.0 or
Glassfish. My Web Service is working correctly and I can access its WSDL
file. I generated all artifacts by using wsimport tool and my client classes
are compiling correctly. Before JAX-WS final release this style of writing
clients was working but now it's not working any more:

package simpleclient;

import helloservice.endpoint.proxy.Hello;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;

public class HelloClient {

    public static void main(String[] args) {
        try {
            HelloClient client = new HelloClient();
            client.doTest(args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doTest(String[] args) {
        try {
            Service service = Service.create(
                    new URL("http://sun02:8080/JAXWS/HelloService?WSDL"),
                    new QName("http://endpoint.helloservice/"));

            System.out.println("Retrieving the port from the following
service: " + service);
            Hello mojProxy = service.getPort(Hello.class);
            System.out.println("Invoking the sayHello operation on the
port.");
            String name;
            if (args.length > 0) {
                name = args[0];
            } else {
                name = "No Name";
            }
            String response = mojProxy.sayHello(name);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

When I run it I get an exception:
Cannot create Service instance, service http://endpoint.helloservice/ not
found in the WSDL http://sun02:8080/JAXWS/HelloService?WSDL
        at
com.sun.xml.ws.client.ServiceContextBuilder.build(ServiceContextBuilder.java
:64)
        at
com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:103)
        at
com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:50)
        at javax.xml.ws.Service.<init>(Service.java:57)
        at javax.xml.ws.Service.create(Service.java:302)
        at simpleclient.HelloClient.doTest(HelloClient.java:29)
        at simpleclient.HelloClient.main(HelloClient.java:21)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

I tried also to write client with @WebServiceRef:
package simpleclient;

import helloservice.endpoint.proxy.Hello;
import helloservice.endpoint.proxy.HelloService;

import javax.xml.ws.WebServiceRef;

public class HelloClientNew {

    @WebServiceRef(wsdlLocation =
"http://sun02:8080/JAXWS/HelloService?WSDL")
        //This is also not working. The same exception
    //_at_WebServiceRef(Hello.class)
    static HelloService service;

    public static void main(String[] argv) {
        try {
            Hello port = service.getHelloPort();
            String response = port.sayHello("Lukasz");
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But this time I get an exception:
java.lang.NullPointerException
        at simpleclient.HelloClientNew.main(HelloClientNew.java:16)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

Any help how to write a client which is working without running ant tasks is
appreciated.

Best regards
Lukasz Chomin