The Java EE 6 Tutorial

A Simple JAX-WS Application Client

The HelloAppClient class is a stand-alone application client that accesses the sayHello method of HelloService. This call is made through a port, a local object that acts as a proxy for the remote service. The port is created at development time by the wsimport task, which generates JAX-WS portable artifacts based on a WSDL file.

Coding the Application Client

    When invoking the remote methods on the port, the client performs these steps:

  1. Uses the generated helloservice.endpoint.HelloService class, which represents the service at the URI of the deployed service’s WSDL file:

    import helloservice.endpoint.HelloService;
    import javax.xml.ws.WebServiceRef;
    
    public class HelloAppClient {
        @WebServiceRef(wsdlLocation = 
          "META-INF/wsdl/localhost_8080/helloservice/HelloService.wsdl")
        private static HelloService service;
  2. Retrieves a proxy to the service, also known as a port, by invoking getHelloPort on the service:

    helloservice.endpoint.Hello port = service.getHelloPort();

    The port implements the SEI defined by the service.

  3. Invokes the port’s sayHello method, passing a string to the service:

    return port.sayHello(arg0);

Here is the full source of HelloAppClient, which is located in the following directory:


tut-install/examples/jaxws/appclient/src/appclient/
package appclient;

import helloservice.endpoint.HelloService;
import javax.xml.ws.WebServiceRef;

public class HelloAppClient {
    @WebServiceRef(wsdlLocation = 
      "META-INF/wsdl/localhost_8080/helloservice/HelloService.wsdl")
    private static HelloService service;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       System.out.println(sayHello("world"));
    }

    private static String sayHello(java.lang.String arg0) {
        helloservice.endpoint.Hello port = service.getHelloPort();
        return port.sayHello(arg0);
    }
}

Building, Packaging, Deploying, and Running the Application Client

You can build, package, deploy, and run the appclient application by using either NetBeans IDE or Ant. To build the client, you must first have deployed helloservice, as described in Building, Packaging, and Deploying the Service.

ProcedureTo Build, Package, Deploy, and Run the Application Client Using NetBeans IDE

  1. In NetBeans IDE, select File->Open Project.

  2. In the Open Project dialog, navigate to:


    tut-install/examples/jaxws/
    
  3. Select the appclient folder.

  4. Select the Open as Main Project check box.

  5. Click Open Project.

  6. In the Projects tab, right-click the appclient project and select Run.

    You will see the output of the application client in the Output pane.

ProcedureTo Build, Package, Deploy, and Run the Application Client Using Ant

  1. In a terminal window, go to:


    tut-install/examples/jaxws/appclient/
    
  2. Type the following command:


    ant
    

    This command calls the default target, which runs the wsimport task and builds and packages the application into a JAR file, appclient.jar, located in the dist directory.

  3. To run the client, type the following command:


    ant run