Creating Service Client with WSDL Document

This section provides a tutorial example on how to create a ServiceClient object with a given WSDL document, a service name and a port name defined in the WSDL document.

Before looking at other Axis2 class, let's take a closer look at how to create a service client object from an existing WSDL document using this constructor method:

   org.apache.axis2.client.ServiceClient client = 
      new ServiceClient(null, wsdlURL, serviceQName, portName)
// "null" indicates a default ConfigurationContext object to be used
// "wsdlURL" specifies a WSDL document using a new URL(wsdlUrlString)
// "serviceQName" specifies a service within the WSDL document
//    using a new QName(tns,serviceName)
//    "tns" specifies the target namespace of the Web service
// "portName" specifies a port within the service using the port name

Now let's write a test program, Axis2ServiceClientHello.java, to create a service client with my hello WSDL document, which is located at http://www.herongyang.com/Service/Hello_WSDL_11_SOAP.wsdl.

/**
 * Axis2ServiceClientHello.java
 * Copyright (c) 2009 by Dr. Herong Yang, herongyang.com
 * All rights reserved
 */
import java.io.PrintStream;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.context.ConfigurationContext;
class Axis2ServiceClientHello {
   public static void main(String[] args) {
      PrintStream out = System.out;

// Setting initial values
      ConfigurationContext config = null;
      String wsdl = "file:///C:/herong/Hello_WSDL_11_SOAP.wsdl";
      String tns = "http://www.herongyang.com/Service/";
      String serviceName = "helloService";
      String portName = "helloPort";

      try {

// Creating a dynamic service client with a given WSDL
         ServiceClient client = new ServiceClient(
            config,
            new URL(wsdl), 
            new QName(tns, serviceName), 
            portName);

// Printing ServiceClient object information
         out.println();
         out.println("ServiceClient object information:");
         out.println("   client: "+client);
         out.println("   client.getAxisConfiguration(): "
            +client.getAxisConfiguration());
         out.println("   client.getAxisService(): "
            +client.getAxisService());
         out.println("   client.getOptions(): "
            +client.getOptions());
         out.println("   client.getServiceContext(): "
            +client.getServiceContext());
         out.println("   client.getTargetEPR(): "
            +client.getTargetEPR());

      } catch (Exception e) {
         e.printStackTrace(); 
      }
   }
}

Compilation and execution of this program are described below:

C:\herong>\local\jdk\bin\javac -Djava.ext.dirs=\local\axis2\lib\
   Axis2ServiceClientHello.java

C:\herong>\local\jdk\bin\java -Djava.ext.dirs=\local\axis2\lib\
   Axis2ServiceClientHello
log4j:WARN No appenders could be found for logger 
   (org.apache.axis2.description.WSDL11ToAxisServiceBuilder).
log4j:WARN Please initialize the log4j system properly.

ServiceClient object information:
   client: org.apache.axis2.client.ServiceClient@1
   client.getAxisConfiguration():
      org.apache.axis2.engine.AxisConfiguration@148bd3
   client.getAxisService(): helloService
   client.getOptions(): org.apache.axis2.client.Options@e80842
   client.getServiceContext():
      org.apache.axis2.context.ServiceContext@17653ae
   client.getTargetEPR(): null

Notes on the test program code and output:

Last update: 2009.

Table of Contents

 About This Book

 Introduction to WSDL 2.0

 WSDL 2.0 Document Structure and Syntax

 WSDL Version 2.0 Part 2: Adjuncts

 WSDL 2.0 Document Examples with SOAP Binding

 WSDL 20 Programming APIs and Testing Tools

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 soapUI 3.0.1 - Web Service Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension in PHP 5.3.1

 Using WSDL in Perl with SOAP::Lite 0.710

Using WSDL Document in Java with Axis2 1.4.1

 What Is Axis2?

 Downloading and Installing Axis2/Java 1.4.1

 org.apache.axis2.client.ServiceClient Class

Creating Service Client with WSDL Document

 org.apache.axis2.client.Options - Operation Client Options

 sendReceive() Method - Invoking a Named Operation

 Turning Off the Chunked HTTP Flag

 AXIOM (AXIs Object Model)

 Axis2RegistrationClient.java - document/literal Style

 Axis2GetSpeechClient.java - document/literal Style

 org.apache.axis2.rpc.client.RPCServiceClient Class

 Axis2GetExchangeRateClient.java - rpc/encoded Style

 SocketRequestResponseServer.java - Socket Server Testing Program

 Capturing the HTTP Request from an Axis2 Client Program

 Using WSDL2Java to Generate Web Service Stub Classes

 WSDL 1.1 Binding Extension for SOAP 1.2

 WSDL 1.1 and SOAP 1.2 Examples - Document and RPC Styles

 SOAP 1.2 Binding - PHP, Java and Perl Clients

 WSDL Related Terminologies

 References

 PDF Printing Version