WSDL2JAVA using AXIS2 code generator command line


The Code Generator tool consists of a command line version and an Ant Task.  I have attached a guidance to use the WSDL2Java utility using the command line.

Also, illustrated how to build file using custom Ant task and invoking the Code Generator from Ant.

Invoking the Code Generator From Ant

Since the users may find altering their ant class path a bit daunting they can also follow an easier technique. The code generator main class can be invoked directly through the build file.

Below is an example of a full build.xml needed to run WSDL2Java and generate the Java source files, compile the sources, and build an AAR file ready for deployment:

<?xml version=”1.0″ encoding=”UTF-8″?>

<project basedir=”.” default=”jar.server”>

<!–Auto generated ant build file–>

<property environment=”env”/>

<property name=”axis2.home” value=”${env.AXIS2_HOME}”/>

<property name=”project.base.dir” value=”.”/>

<property name=”maven.class.path” value=””/>

<property name=”name” value=”ExaminerManagementService”/>

<property name=”src” value=”${project.base.dir}/src”/>

<property name=”test” value=”${project.base.dir}/test”/>

<property name=”build” value=”${project.base.dir}/build”/>

<property name=”classes” value=”${build}/classes”/>

<property name=”lib” value=”${build}/lib”/>

<property name=”resources” value=”${project.base.dir}/resources”/>

<property value=”XBeans-packaged.jar” name=”xbeans.packaged.jar.name”/>

<property value=”” name=”jars.ok”/>

<path id=”axis2.class.path”>

<pathelement path=”${java.class.path}”/>

<pathelement path=”${maven.class.path}”/>

<fileset dir=”${axis2.home}”>

<include name=”lib/*.jar”/>

</fileset>

</path>

<target name=”init”>

<mkdir dir=”${build}”/>

<mkdir dir=”${classes}”/>

<mkdir dir=”${lib}”/>

</target>

<target name=”jar.xbeans”>

<jar basedir=”${resources}” destfile=”${lib}/${xbeans.packaged.jar.name}” excludes=”**/services.xml, **/*.xsd”/>

</target>

<target depends=”init, jar.xbeans” name=”pre.compile.test”>

<!–Test the classpath for the availability of necesary classes–>

<available classpathref=”axis2.class.path” property=”xbeans.available” classname=”org.apache.xmlbeans.XmlObject”/>

<available classpathref=”axis2.class.path” property=”stax.available” classname=”javax.xml.stream.XMLStreamReader”/>

<available classpathref=”axis2.class.path” property=”axis2.available” classname=”org.apache.axis2.engine.AxisEngine”/>

<condition property=”jars.ok”>

<and>

<isset property=”xbeans.available”/>

<isset property=”stax.available”/>

<isset property=”axis2.available”/>

</and>

</condition>

<!–Print out the availabilities–>

<echo message=”XmlBeans Availability = ${xbeans.available}”/>

<echo message=”Stax Availability= ${stax.available}”/>

<echo message=”Axis2 Availability= ${axis2.available}”/>

</target>

<target depends=”pre.compile.test” name=”compile.src” if=”jars.ok”>

<javac debug=”on” memoryMaximumSize=”256m” memoryInitialSize=”256m” fork=”true” destdir=”${classes}” srcdir=”${src}”>

<classpath location=”${lib}/${xbeans.packaged.jar.name}”/>

<classpath refid=”axis2.class.path”/>

</javac>

</target>

<target depends=”compile.src” name=”compile.test” if=”jars.ok”>

<javac debug=”on” memoryMaximumSize=”256m” memoryInitialSize=”256m” fork=”true” destdir=”${classes}”>

<src path=”${test}”/>

<classpath location=”${lib}/${xbeans.packaged.jar.name}”/>

<classpath refid=”axis2.class.path”/>

</javac>

</target>

<target depends=”pre.compile.test” name=”echo.classpath.problem” unless=”jars.ok”>

<echo message=”The class path is not set right!                                Please make sure the following classes are in the classpath                                1. XmlBeans                                2. Stax                                3. Axis2                 “/>

</target>

<target depends=”jar.server, jar.client” name=”jar.all”/>

<target depends=”compile.src,echo.classpath.problem” name=”jar.server” if=”jars.ok”>

<copy toDir=”${classes}/META-INF” failonerror=”false”>

<fileset dir=”${resources}”>

<include name=”*.xml”/>

<include name=”*.wsdl”/>

<include name=”*.xsd”/>

<exclude name=”**/schemaorg_apache_xmlbean/**”/>

</fileset>

</copy>

<copy file=”${lib}/${xbeans.packaged.jar.name}” toDir=”${classes}/lib”/>

<jar destfile=”${lib}/${name}.aar”>

<fileset excludes=”**/Test.class” dir=”${classes}”/>

</jar>

</target>

<target if=”jars.ok” name=”jar.client” depends=”compile.src”>

<jar destfile=”${lib}/${name}-test-client.jar”>

<fileset dir=”${classes}”>

<exclude name=”**/META-INF/*.*”/>

<exclude name=”**/lib/*.*”/>

<exclude name=”**/*MessageReceiver.class”/>

<exclude name=”**/*Skeleton.class”/>

</fileset>

<fileset dir=”${resources}”>

<exclude name=”**/*.wsdl”/>

<exclude name=”**/*.xsd”/>

<exclude name=”**/*.xml”/>

</fileset>

</jar>

</target>

<target if=”jars.ok” depends=”jar.server” name=”make.repo”>

<mkdir dir=”${build}/repo/”/>

<mkdir dir=”${build}/repo/services”/>

<copy file=”${build}/lib/${name}.aar” toDir=”${build}/repo/services/”/>

</target>

<target if=”jars.ok” depends=”make.repo” name=”start.server”>

<java fork=”true” classname=”org.apache.axis2.transport.http.SimpleHTTPServer”>

<arg value=”${build}/repo”/>

<classpath refid=”axis2.class.path”/>

</java>

</target>

<target if=”jars.ok” depends=”jar.client” name=”run.test”>

<path id=”test.class.path”>

<pathelement location=”${lib}/${name}-test-client.jar”/>

<path refid=”axis2.class.path”/>

</path>

<mkdir dir=”${build}/test-reports/”/>

<junit haltonfailure=”yes” printsummary=”yes”>

<classpath refid=”test.class.path”/>

<formatter type=”plain”/>

<batchtest fork=”yes” toDir=”${build}/test-reports/”>

<fileset dir=”${test}”>

<include name=”**/*Test*.java”/>

</fileset>

</batchtest>

</junit>

</target>

<target name=”clean”>

<delete dir=”${build}”/>

</target>

</project>

The above build.xml depends on a build.properties file which defines ‘axis.home’, such as:

axis.home=/home/username/axis2-1.0-bin/

The above build.xml example also assumes three empty directories exist, ‘dist’, ‘lib’, and ‘src’.

Below is a validated WSDL Document following the Document/Literal Style. The name of this file matches the name used in the WSDL2Java ant task above, ExaminerManagementService.wsdl

<?xml version=“1.0” encoding=“UTF-8” standalone=“no”?>

<wsdl:definitions xmlns:soap=http://schemas.xmlsoap.org/wsdl/soap12/&#8221;

xmlns:tns=http://www.nysolutionsltd.com/ExaminerManagementService/&#8221;

xmlns:soapbind=http://schemas.xmlsoap.org/wsdl/soap12/&#8221; xmlns:wsdl=http://schemas.xmlsoap.org/wsdl/&#8221;

xmlns:xsd=http://www.w3.org/2001/XMLSchema&#8221; name=“ExaminerManagementService”

targetNamespace=http://www.nysolutionsltd.com/ExaminerManagementService/&#8221;>

<wsdl:types>

<xsd:schema targetNamespace=http://www.nysolutionsltd.com/ExaminerManagementService/&#8221;>

<xsd:element name=“GetPaymentDetailsRequest”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“PaymentID” type=“xsd:string” />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name=“GetPaymentDetailsResponse”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“out” type=“xsd:string” />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name=“GetExaminerPaymentsRequest”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“ExaminerAccountCode” type=“xsd:string”

maxOccurs=“1” minOccurs=“1”>

</xsd:element>

<xsd:element name=“AllocationMode” type=“xsd:string”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“JournalSource” type=“xsd:string”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“AccountingPeriodStartDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“TransactionDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“AccountingPeriodEndDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:element name=“GetExaminerPaymentsResponse”>

<xsd:complexType>

<xsd:sequence>

<xsd:element name=“ExaminerAccountCode” type=“xsd:string”

maxOccurs=“1” minOccurs=“1”>

</xsd:element>

<xsd:element name=“AllocationMode” type=“xsd:string”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“JournalSource” type=“xsd:string”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“AccountingPeriodStartDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“TransactionDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“AccountingPeriodEndDate” type=“xsd:dateTime”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

<xsd:element name=“CurrencyCode” type=“xsd:string”

maxOccurs=“1” minOccurs=“1”>

</xsd:element>

<xsd:element name=“Payments” type=“tns:PaymentsType”

maxOccurs=“unbounded” minOccurs=“0”>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:element>

<xsd:complexType name=“PaymentsType”>

<xsd:sequence>

<xsd:element name=“paymentID” type=“xsd:string”

maxOccurs=“1” minOccurs=“1”>

</xsd:element>

<xsd:element name=“paymentRef” type=“xsd:string”

maxOccurs=“1” minOccurs=“0”>

</xsd:element>

</xsd:sequence>

</xsd:complexType>

</xsd:schema>

</wsdl:types>

<wsdl:message name=“GetPaymentDetailsRequest”>

<wsdl:part element=“tns:GetPaymentDetailsRequest” name=“parameters” />

</wsdl:message>

<wsdl:message name=“GetPaymentDetailsResponse”>

<wsdl:part element=“tns:GetPaymentDetailsResponse” name=“parameters” />

</wsdl:message>

<wsdl:message name=“GetExaminerPaymentsRequest”>

<wsdl:part name=“parameters” element=“tns:GetExaminerPaymentsRequest”>

</wsdl:part>

</wsdl:message>

<wsdl:message name=“GetExaminerPaymentsResponse”>

<wsdl:part name=“parameters” element=“tns:GetExaminerPaymentsResponse”>

</wsdl:part>

</wsdl:message>

<wsdl:portType name=“ExaminerManagementService”>

<wsdl:operation name=“GetPaymentDetails”>

<wsdl:input message=“tns:GetPaymentDetailsRequest” />

<wsdl:output message=“tns:GetPaymentDetailsResponse” />

</wsdl:operation>

<wsdl:operation name=“GetExaminerPayments”>

<wsdl:input message=“tns:GetExaminerPaymentsRequest”></wsdl:input>

<wsdl:output message=“tns:GetExaminerPaymentsResponse”></wsdl:output>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name=“ExaminerManagementServiceSOAP” type=“tns:ExaminerManagementService”>

<soap:binding style=“document”

transport=http://schemas.xmlsoap.org/soap/http&#8221; />

<wsdl:operation name=“GetPaymentDetails”>

<soap:operation

soapAction=http://www.nysolutionsltd.com/ExaminerManagementService/GetPaymentDetails&#8221; />

<wsdl:input>

<soap:body use=“literal” />

</wsdl:input>

<wsdl:output>

<soap:body use=“literal” />

</wsdl:output>

</wsdl:operation>

<wsdl:operation name=“GetExaminerPayments”>

<soap:operation

soapAction=http://www.nysolutionsltd.com/ExaminerManagementService/GetExaminerPaymentss&#8221; />

<wsdl:input>

<soap:body use=“literal” />

</wsdl:input>

<wsdl:output>

<soap:body use=“literal” />

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name=“ExaminerManagementService”>

<wsdl:port binding=“tns:ExaminerManagementServiceSOAP” name=“ExaminerManagementServiceSOAP”>

<soap:address location=http://www.nysolutionsltd.com/&#8221; />

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

Place the above file, named ExaminerManagementService.wsdl, in the directory ‘wsdl’ below the build.xml file. Run the WSDL2Java command via the ant task defined above, and there will be a directory called ‘output’ created. This directory contains the WSDL2Java generated source. An important detail is that an XMLBean class file is also generated by WSDL2Java, TypeSystemHolder.class. That file is placed into build/classes by the above ant task and will be needed to compile the generated sources. A frequent problem is users get an error such as:

ClassNotFoundException : Cannot load SchemaTypeSystem. Unable to load class with name schemaorg_apache_xmlbeans.system.s68C41DB812F52C975439BA10FE4FEE54.TypeSystemHolder. Make sure the generated binary files are on the classpath.

The TypeSystemHolder.class generated by WSDL2Java must be placed in your classpath in order to avoid this error.

The next step is to modify the generated Skeleton Java Source file – the Web service. This file as generated returns null and needs to be updated to contain the business logic.

After the WSDL2Java command runs the file ExaminerManagementService.wsdl, edit the following file:

output/org/example/types/ExaminerManagementServiceSkeleton.java. You should see the following code:

/**
* ExaminerManagementServiceSkeleton.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 1.0-RC4 Apr 28, 2006 (05:23:23 IST)
*/
package org.example.types;
/**
*  ExaminerManagementServiceSkeleton java skeleton for the axisService
*/
public class ExaminerManagementServiceSkeleton {

/**
* Auto generated method signature

* @param param0

*/
public  login.types.ReturnWebLoginElementDocument webLogin
(login.types.WebLoginElementDocument param0 )

{
//Todo fill this with the necessary business logic
throw new  java.lang.UnsupportedOperationException();
}

}

Replace the contents of this file with the following, which uses the complex types generated by WSDL2Java and the example wsdl file:

/**
* ExaminerManagementServiceSkeleton.java
*
* This file was auto-generated from WSDL
* by the Apache Axis2 version: 1.0-RC4 Apr 28, 2006 (05:23:23 IST)
*/
package org.example.types;
import login.types.ReturnWebLoginElementDocument;
import login.types.ReturnWebLoginElementDocument.*;
import login.types.WebLoginElementDocument;
import login.types.WebLoginElementDocument.*;

/**
*  Auto generated java skeleton for the service by the Axis code generator
*/
public class ExaminerManagementServiceSkeleton {

/**
* Auto generated method signature

* @param webLoginElementDocument changed from param0

*/
public ReturnWebLoginElementDocument webLogin
(WebLoginElementDocument webLoginElementDocument ){

//Todo fill this with the necessary business logic
System.out.println(“ExaminerManagementServiceSkeleton.webLogin reached successfully!”);

// Get parameters passed in
WebLoginElement webLoginElement = webLoginElementDocument.getWebLoginElement();
String userName = webLoginElement.getUserNameElement();
String password = webLoginElement.getUserPasswordElement();
System.out.println(“ExaminerManagementServiceSkeleton.webLogin userName: ” + userName);
System.out.println(“ExaminerManagementServiceSkeleton.webLogin password: ” + password);

// input paramaters would be used here

// prepare output
ReturnWebLoginElementDocument retDoc =
ReturnWebLoginElementDocument.Factory.newInstance();

ReturnWebLoginElement retElement = ReturnWebLoginElement.Factory.newInstance();

retElement.setWebUserNameElement(“joe sixpack”);
retElement.setSoapSessionIdElement(“some_random_string”);
System.out.println(“validate retElement: ” + retElement.validate());

retDoc.setReturnWebLoginElement(retElement);
System.out.println(“validate retDoc: ” + retDoc.validate());

System.out.println(“ExaminerManagementServiceSkeleton.webLogin returning…”);

return retDoc;

}
}

The next steps assume the axis2.war has been deployed and has expanded in a servlet container.

Run the ‘jar_wsdl’ ant task from the example build.xml, which generates a jar file lib/axis2_example_wsdl.jar in the ‘lib’ directory under the build.xml . This jar will be used to compile the client, and also will be placed in the servlet container. Next, run the ‘aar’ ant task from the example build.xml, which generates the deployable axis2 Web service. Place dist/LoginEndpoint.aar into axis2/WEB-INF/services . Place lib/axis2_example_wsdl.jar into axis2/WEB-INF/lib . Verify the happy axis page loaded the services correctly – there should be the service ‘LoginEndpoint’ with the available operation ‘webLogin’ displayed.

The last step is to create and run the client. In the src directory create the file org.client.LoginClient.java, with the contents below:

package org.client;

import org.apache.axis2.AxisFault;

import login.types.ReturnWebLoginElementDocument;
import login.types.ReturnWebLoginElementDocument.*;
import login.types.WebLoginElementDocument;
import login.types.WebLoginElementDocument.*;
import org.example.types.ExaminerManagementServiceStub;

/**
* Login.
*
*/
public class LoginClient {

public static void main(String[] args) {
try {

System.out.println(“webLogin, firing…”);
ExaminerManagementServiceStub stub = new ExaminerManagementServiceStub();

WebLoginElementDocument webLoginElementDocument
= WebLoginElementDocument.Factory.newInstance();
WebLoginElement webLoginElement =
WebLoginElement.Factory.newInstance();
webLoginElement.setUserNameElement(“joe”);
webLoginElement.setUserPasswordElement(“sixpack”);

webLoginElementDocument.setWebLoginElement(webLoginElement);

System.out.println(“validate: ” +  webLoginElement.validate());
stub.webLogin(webLoginElementDocument);

ReturnWebLoginElementDocument returnWebLoginElementDocument =
stub.webLogin(webLoginElementDocument);

System.out.println(“Client returned”);

ReturnWebLoginElementDocument.ReturnWebLoginElement
retElement = returnWebLoginElementDocument.getReturnWebLoginElement();

System.out.println(“WebUserName: ” + retElement.getWebUserNameElement());
System.out.println(“SOAPSessionId: ” + retElement.getSoapSessionIdElement());
System.out.println(“webLogin, completed!!!”);

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Now run the ant task ‘ant runLogin’ . The following output should appear:

runLogin:
[echo] running the webLogin client
[java] webLogin, firing…
[java] validate: true
[java] Client returned
[java] WebUserName: joe sixpack
[java] SOAPSessionId: some_random_string
[java] webLogin, completed!!!

2 comments

  1. com: Forrester Research is holding its own conference down in Orlando and has just revealed
    its predictions for the growth of online advertising.
    Today there are hundreds of social media websites like Facebook, You –
    Tube, Twitter and Digg, each of which caters to a multitude of different and overlapping audiences.
    Its cost-effectiveness has dragged people towards it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s