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.

According to the manual, Java Axis2 ServicClient class seems to support WSDL 2.0. See the comment on the portName parameter of the constructor method:

public ServiceClient(ConfigurationContext configContext,
                     java.net.URL wsdlURL,
                     QName wsdlServiceName,
                     java.lang.String portName)
              throws AxisFault

    Create a service client for WSDL service identified by the QName
    of the wsdl:service element in a WSDL document.

    Parameters:
        configContext - The configuration context under which this
           service lives (may be null, in which case a new local one
           will be created)
        wsdlURL - The URL of the WSDL document to read
        wsdlServiceName - The QName of the WSDL service in the WSDL
           document to create a client for
        portName - The name of the WSDL 1.1 port to create a client
           for. May be null (if WSDL 2.0 is used or if only one port
           is there).

Here is a test program, Axis2ServiceClientHelloWsdl20.java, that loads my WSDL 2.0 document, Hello_WSDL_20_SOAP.wsdl

/* Axis2ServiceClientHelloWsdl20.java
 * Copyright (c) 2009 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;
import org.apache.axiom.om.OMElement;

class Axis2ServiceClientHelloWsdl20 {
   public static void main(String[] args) {
      PrintStream out = System.out;

      ConfigurationContext config = null;
      String wsdl = "file:///C:/herong/Hello_WSDL_20_SOAP.wsdl";
      String tns = "https://www.herongyang.com/Service/";
      String serviceName = "helloService";
      String portName = null;

      try {

         ServiceClient client = new ServiceClient(config,
            new URL(wsdl), new QName(tns, serviceName), portName);

         OMElement request = null;
         OMElement response = client.sendReceive(
          new QName(tns, "Hello"), request);
       out.println(response);

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

I can compile this program with the "-Djava.ext.dirs=\local\axis2\lib\" option with JDK 1.8 or earlier versions, so it will automatically load all JAR files in that directory in the class path.

But with JDK 9 or higher, I got this error:

herong> javac -Djava.ext.dirs=\local\axis2\lib\ \
   Axis2ServiceClientHello.java

javac: option -extdirs not allowed with target 1.10

One workaround is the use "-cp .;\local\axis2\lib\*" option:

herong> javac -cp .;\local\axis2\lib\* Axis2ServiceClientHelloWsdl20.java

herong> java -cp .;\local\axis2\lib\* Axis2ServiceClientHelloWsdl20

log4j:WARN No appenders could be found for logger ...
log4j:WARN Please initialize the log4j system properly.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by
   org.apache.ws.commons.schema.utils.DOMUtil
   (file:/C:/local/axis2/lib/xmlschema-core-2.2.1.jar)
   to method com.sun.org.apache.xerces.internal.dom
   .CoreDocumentImpl.getXmlEncoding()
WARNING: Please consider reporting this to the maintainers of
   org.apache.ws.commons.schema.utils.DOMUtil
WARNING: Use --illegal-access=warn to enable warnings of further
   illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
log4j:WARN No appenders could be found for logger
   (org.apache.axiom.locator.DefaultOMMetaFactoryLocator).
log4j:WARN Please initialize the log4j system properly.

<hy:HelloResponse xmlns:hy="https://www.herongyang.com/Service/">
      Hello from server - herongyang.com.
    </hy:HelloResponse>

Cool. Apache Axis2/Java 1.7.8 does support WSDL 2.0 documents!

Earlier when I compiled and run it with Axis2 1.4.1, I was getting the following error:

herong> \local\jdk\bin\java -Djava.ext.dirs=\local\axis2\lib\
   Axis2ServiceClientHelloWsdl20

org.apache.axis2.AxisFault: WSDLException (at /wsdl:description):
   faultCode=INVALID_WSDL: Expected element
   '{http://schemas.xmlsoap.org/wsdl/}definitions'.
   at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)
   at org.apache.axis2.description.AxisService.createClientSideAxi...
   at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.j...
   at Axis2ServiceClientHelloWsdl20.main(Axis2ServiceClientHelloWs...
Caused by: javax.wsdl.WSDLException: WSDLException (at /wsdl:descr...
   at com.ibm.wsdl.xml.WSDLReaderImpl.checkElementName(Unknown Sou...
   at com.ibm.wsdl.xml.WSDLReaderImpl.parseDefinitions(Unknown Sou...
   at com.ibm.wsdl.xml.WSDLReaderImpl.readWSDL(Unknown Source)

The ServiceClient constructor method is expecting a WSDL 1.1 document by default. I could not find a way to change this default behavior.

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

Using WSDL Document in Java Apache Axis2/Java for WSDL

 What Is Axis2

 Downloading and Installing Axis2/Java

 org.apache.axis2.client.ServiceClient Class

Creating Service Client with WSDL Document

  WSDL2Java Converting WSDL 2.0 Documents to Stub Classes

 Apache Woden for WSDL Documents in Java

 SoapUI - Web Service Testing Tool

 PHP SOAP Extension for WSDL

 Perl SOAP::Lite for WSDL

 Introduction to WSDL 1.1

 WSDL 1.1 Document Structure and Syntax

 WSDL 1.1 Binding Extension for SOAP 1.1

 SoapUI as WSDL 1.1 Testing Tool

 WSDL 1.1 and SOAP 1.1 Examples - Document and RPC Styles

 PHP SOAP Extension for WSDL 1.1

 Perl SOAP::Lite for WSDL 1.1

 Apache Axis2/Java for WSDL 1.1

 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

 Python SOAP Client: Zeep

 WSDL Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB