Skip to main content Skip to complementary content

Implementing a CXF Client

This section describes how to write the code for a simple Java client, based on the WSDL contract above. To implement the client, you need to use the following stub classes:

  • Service class (that is, SOAPService ).

  • Service endpoint interface (that is, Greeter ).

Generated service class

The next example shows the typical outline a generated service class, ServiceName , which extends the javax.xml.ws.Service base class.

Outline of a Generated Service Class

public class ServiceName extends javax.xml.ws.Service {
            ...
            public ServiceName(URL wsdlLocation, QName serviceName) { } 
            public ServiceName() { }
            public Greeter getPortName() { }
            ...
            }

The ServiceName class above defines the following methods:

  • Constructor methods - the following forms of constructor are defined:

    • ServiceName(URL wsdlLocation, QName serviceName) constructs a service object based on the data in the serviceName service in the WSDL contract that is obtainable from wsdlLocation .

    • ServiceName() is the default constructor, which constructs a service object based on the service name and WSDL contract that were provided at the time the stub code was generated (for example, when running the CXF wsdl2java command). Using this constructor presupposes that the WSDL contract remains available at its original location.

  • get_PortName_() methods - for every PortName port defined on the ServiceName service, CXF generates a corresponding get_PortName_() method in Java. Therefore, a wsdl:service element that defines multiple ports will generate a service class with multiple get_PortName_() methods.

Service endpoint interface

About this task

For every port type defined in the original WSDL contract, you can generate a corresponding service endpoint interface in Java. A service endpoint interface is the Java mapping of a WSDL port type. Each operation defined in the original WSDL port type maps to a corresponding method in the service endpoint interface. The operation's parameters are mapped as follows:

Procedure

  1. The input parameters are mapped to method arguments.
  2. The first output parameter is mapped to a return value.
  3. If there is more than one output parameter, the second and subsequent output parameters map to method arguments (moreover, the values of these arguments must be passed using Holder types).

Results

The next example shows the Greeter service endpoint interface, which is generated from the Greeter port type defined in the WSDL above. For simplicity, this example omits the standard JAXB and JAX-WS annotations.

The Greeter Service Endpoint Interface

/* Generated by WSDLToJava Compiler. */
               
               package org.objectweb.hello_world_soap_http;
               ...
               public interface Greeter {
               public java.lang.String sayHi(); 
               public java.lang.String greetMe(java.lang.String requestType);
               public void greetMeOneWay(java.lang.String requestType);
               public void pingMe() throws PingMeFault;
               }

Client Implementation Code

About this task

This example shows the Java code that implements the HelloWorld client. In summary, the client connects to the SoapPort port on the SOAPService service and then proceeds to invoke each of the operations supported by the Greeter port type.

package demo.hw.client;
               
               import java.io.File;
               import java.net.URL;
               import javax.xml.namespace.QName;
               import org.apache.hello_world_soap_http.Greeter;
               import org.apache.hello_world_soap_http.PingMeFault;
               import org.apche.hello_world_soap_http.SOAPService;
               
               public final class Client {
               
               private static final QName SERVICE_NAME = 
               new QName("http://apache.org/hello_world_soap_http", 
               "SOAPService");
               
               private Client() {}
               
               public static void main(String args[]) throws Exception {
               if (args.length == 0) {
               System.out.println("please specify wsdl");
               System.exit(1);
               }
               
               URL wsdlURL;
               File wsdlFile = new File(args[0]);
               if (wsdlFile.exists()) {
               wsdlURL = wsdlFile.toURL();
               } else {
               wsdlURL = new URL(args[0]);
               }
               
               System.out.println(wsdlURL);
               SOAPService ss = new SOAPService(wsdlURL, SERVICE_NAME);
               Greeter port = ss.getSoapPort();
               String resp;
               
               System.out.println("Invoking sayHi...");
               resp = port.sayHi();
               System.out.println("Server responded with: " + resp);
               System.out.println();
               
               System.out.println("Invoking greetMe...");
               resp = port.greetMe(System.getProperty("user.name"));
               System.out.println("Server responded with: " + resp);
               System.out.println();
               
               System.out.println("Invoking greetMeOneWay...");
               port.greetMeOneWay(System.getProperty("user.name"));
               System.out.println("No response from server as method is OneWay");
               System.out.println();
               
               try {
               System.out.println("Invoking pingMe, expecting exception...");
               port.pingMe();
               } catch (PingMeFault ex) {
               System.out.println(
               "Expected exception: PingMeFault has occurred.");
               System.out.println(ex.toString());
               }
               System.exit(0);
               }
               }

The Client.main() function from the above example proceeds as follows:

Procedure

  1. The CXF runtime is implicitly initialized - that is, provided the CXF runtime classes are loaded. Hence, there is no need to call a special function in order to initialize CXF.
  2. The client expects a single string argument that gives the location of the WSDL contract for HelloWorld. The WSDL location is stored in wsdlURL .
  3. A new port object (which enables you to access the remote server endpoint) is created in two steps, as shown in the following code fragment:
    SOAPService ss = new SOAPService(wsdlURL, SERVICE_NAME);
                         Greeter port = ss.getSoapPort();
    To create a new port object, you first create a service object (passing in the WSDL location and service name) and then call the appropriate get PortName () method to obtain an instance of the particular port you need. In this case, the SOAPService service supports only the SoapPort port, which is of Greeter type.
  4. The client proceeds to call each of the methods supported by the Greeter service endpoint interface.
  5. In the case of the pingMe() operation, the example code shows how to catch the PingMeFault fault exception.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!