Tuesday, July 5, 2011

Convert WSDL to Java for calling SOAP web services interfaces using Ant Script

Another blog regarding web services, this time with SOAP web services.
SOAP, in simple terms, is a simple XML-based protocol to let applications exchange information over HTTP. SOAP is a protocol for accessing a Web Service and it stands for SOAP stands for Simple Object Access Protocol. I am not going in to the concepts of SOAP web services as they will be few clicks away. Here mainly, will highlight in code level.
For the next few blog will be how to invoke a SOAP web services and get the response from them.
We are using AXIS-2 framework for this.
@Credit: Sumeet Chakraborty, who has contributed to “my learning” of web services immensely.
Whenever, you are required to call a SOAP based web services, you are given the wsdl file – which is nothing but a XML file with all the specifications, exceptions handled at server end.
Our first job is to create java files and then create jar files out of that wsdl file. I am using ant script [build.xml] to do so.
Extract from the build.xml file for the convertion.
Converting of wsdl to java files:
<target name="VOSP_wsdl2java" depends="clean,prepare">
            <java classname="org.apache.axis2.wsdl.WSDL2Java" fork="true">
                        <classpath refid="refer_classpath" />
                        <arg value="-d" />
                        <arg value="xmlbeans" />
                        <arg value="-uri" />
                        <arg file="sample.wsdl" />
                        <arg value="-o" />
                        <arg file="output" />
                        <arg value="-p" />
                        <arg value="com.project.types" />
                  </java>
                  <!-- Move the schema folder to classpath-->
                  <move todir="${build.soap.classes}">
                        <fileset dir="output/resources">
                              <include name="**/*schema*/**/*.class" />
                              <include name="**/*schema*/**/*.xsb" />
                        </fileset>
                  </move>

</target>



Compiling the java files:
<target name="soap_compile">
            <echo message="Compiling wsdl2Java files" />
            <javac srcdir="output" destdir="${build.soap.classes}" deprecation="true" failonerror="true" debug="true">

                  <classpath refid="refer_classpath" />
                  </javac>

</target>
Create the jar file:
<target name="soap_client_jar" depends="soap_compile">
      <jar jarfile="dist/ServiceClient.jar">
            <fileset dir="${build.soap.classes}" />
      </jar>
      <copy todir="lib" overwrite="true">
            <fileset dir="dist">
                  <include name="*.jar" />
            </fileset>
      </copy>
</target>
Jars Required:
axiom-api-1.2.10.jar
axiom-dom-1.2.10.jar
axiom-impl-1.2.10.jar
axis2-adb-1.5.4.jar
axis2-adb-codegen-1.5.4.jar
axis2-ant-plugin-1.5.4.jar
axis2-clustering-1.5.4.jar
axis2-codegen-1.5.4.jar
axis2-corba-1.5.4.jar
axis2-fastinfoset-1.5.4.jar
 axis2-java2wsdl-1.5.4.jar
axis2-jaxbri-1.5.4.jar
axis2-jaxws-1.5.4.jar
axis2-jibx-1.5.4.jar
axis2-json-1.5.4.jar
axis2-kernel-1.5.4.jar
axis2-metadata-1.5.4.jar
axis2-mtompolicy-1.5.4.jar
axis2-saaj-1.5.4.jar
axis2-soapmonitor-servlet-1.5.4.jar
axis2-spring-1.5.4.jar
axis2-transport-http-1.5.4.jar
axis2-transport-local-1.5.4.jar
axis2-xmlbeans-1.5.4.jar
commons-logging-1.1.1.jar
neethi-2.0.4.jar
wsdl4j-1.6.2.jar
xercesImpl-2.8.1.jar
xml-resolver-1.2.jar
xmlbeans-2.3.0.jar
XmlSchema-1.4.3.jar
activation-1.1.jar
commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
httpcore-4.0.jar
mail-1.4.jar

At the end of this script run, you will get ServiceClient.jar which can be used to call the service.

Hope you will find it useful.

2 comments:

Unknown said...

No Offence but where are the steps for the conversion of wsdl(xml)to java

aditya55 said...

SQL AND & OR Operators

Total Pageviews