wsimport is a tool shipped with JDK/JRE and is present in the “bin” folder of JDK/JRE. The purpose of this small utility is to take wsdl file as input and generate JAX-WS artifacts for building the service endpoint infrastructure code. It can also generate the required exception classes by looking at the faults definition present in the wsdl. There are a number of options that can be used with this tool.

Let us now go through the steps to use wsimport tool in Eclipse as an ant task.

Project Structure:

The following screenshot shows how the project looks like before we start:

A few points to note here:

  • wsdl_xsd folder contains the wsdl and xsd files for the web service.
  • wsimportclasses and wsimportsrc folders will contain the generated class and java files after we run the ant build tasks. These classes will help in writing the web service end point interface and client code.

WSDL for web service

The following is the WSDL file from where we start and build up the web service.

<?xml version="1.0" encoding="UTF-8"?>
<definitions
	xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
	xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
	xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://sample.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://sample.com/"
	name="StockPriceImplService">
	<wsp:Policy wsu:Id="StockServicePortBinding_MTOM_Policy">
		<ns1:OptimizedMimeSerialization
			xmlns:ns1="http://schemas.xmlsoap.org/ws/2004/09/policy/optimizedmimeserialization"
			wsp:Optional="true" />
	</wsp:Policy>
	<types>
		<xsd:schema>
			<xsd:import namespace="http://sample.com/" schemaLocation="./com.sample_xsd.xml" />
		</xsd:schema>
	</types>
	<message name="getStockPrice">
		<part name="parameters" element="tns:getStockPrice" />
	</message>
	<message name="getStockPriceResponse">
		<part name="parameters" element="tns:getStockPriceResponse" />
	</message>
	<message name="downloadImage">
		<part name="parameters" element="tns:downloadImage" />
	</message>
	<message name="downloadImageResponse">
		<part name="parameters" element="tns:downloadImageResponse" />
	</message>
	<portType name="StockService">
		<operation name="getStockPrice">
			<input wsam:Action="http://sample.com/StockService/getStockPriceRequest"
				message="tns:getStockPrice" />
			<output wsam:Action="http://sample.com/StockService/getStockPriceResponse"
				message="tns:getStockPriceResponse" />
		</operation>
		<operation name="downloadImage">
			<input wsam:Action="http://sample.com/StockService/downloadImageRequest"
				message="tns:downloadImage" />
			<output wsam:Action="http://sample.com/StockService/downloadImageResponse"
				message="tns:downloadImageResponse" />
		</operation>
	</portType>
	<binding name="StockServicePortBinding" type="tns:StockService">
		<wsp:PolicyReference URI="#StockServicePortBinding_MTOM_Policy" />
		<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
			style="document" />
		<operation name="getStockPrice">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal" />
			</input>
			<output>
				<soap:body use="literal" />
			</output>
		</operation>
		<operation name="downloadImage">
			<soap:operation soapAction="" />
			<input>
				<soap:body use="literal" />
			</input>
			<output>
				<soap:body use="literal" />
			</output>
		</operation>
	</binding>
	<service name="StockPriceImplService">
		<port name="StockServicePort" binding="tns:StockServicePortBinding">
			<soap:address location="http://localhost:9999/com.sample" />
		</port>
	</service>
</definitions>

XSD for data types

As you would have noticed, the above WSDL specifies document/literal for encoding and hence the data types are externalized into an XSD which is listed below:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:tns="http://sample.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
	version="1.0" targetNamespace="http://sample.com/">

	<xs:element name="downloadImage" type="tns:downloadImage" />

	<xs:element name="downloadImageResponse" type="tns:downloadImageResponse" />

	<xs:element name="getStockPrice" type="tns:getStockPrice" />

	<xs:element name="getStockPriceResponse" type="tns:getStockPriceResponse" />

	<xs:complexType name="downloadImage">
		<xs:sequence>
			<xs:element name="arg0" type="xs:string" minOccurs="0" />
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="downloadImageResponse">
		<xs:sequence>
			<xs:element name="return" type="xs:base64Binary"
				minOccurs="0" />
		</xs:sequence>
	</xs:complexType>

	<xs:complexType name="getStockPrice">
		<xs:sequence />
	</xs:complexType>

	<xs:complexType name="getStockPriceResponse">
		<xs:sequence>
			<xs:element name="return" type="xs:string" minOccurs="0" />
		</xs:sequence>
	</xs:complexType>
</xs:schema>

Ant build.xml task

The build.xml shown in the project structure has the following contents. Here we are invoking the wsimport.exe tool which comes with JDK/JRE.

<project default="wsimport">
  <property name="jdk.home" value="C:/Program Files (x86)/Java/jdk1.6.0_33" />

  <target name="wsimport">
    <exec executable="${jdk.home}/bin/wsimport">
      <arg line="-keep -s ../wsimportsrc -p com.sample 
        -d ../wsimportclasses ../wsdl_xsd/com.sample_wsdl.xml"/>
    </exec>
  </target>
</project>

If you want to know the meaning of various arguments used above, then set the JAVA_HOME enviornment variable and issue wsimport command on the console. -s and -d arguments are used to specify the location of generated .java and .class files.

Running the ant task in eclipse

Build output on the console is:

Buildfile: C:UserssandbhanExtraMyWebServicebuildbuild.xml
wsimport:
     [exec] parsing WSDL...
     [exec] generating code...
     [exec] comsampleDownloadImage.java
     [exec] comsampleDownloadImageResponse.java
     [exec] comsampleGetStockPrice.java
     [exec] comsampleGetStockPriceResponse.java
     [exec] comsampleObjectFactory.java
     [exec] comsampleStockPriceImplService.java
     [exec] comsampleStockService.java
     [exec] comsamplepackage-info.java
     [exec] compiling code...
     [exec] javac -d C:UserssandbhanExtraMyWebServicebuild..wsimportclasses -classpath C:Program Files 
     -----
     -----
BUILD SUCCESSFUL
Total time: 3 seconds

After running the ant task, refresh the project in eclipse (select the project in project explorer and press F5 key)

Generated files

The following screenshot shows the .java and .class files generated by the wsimport.

DownloadImage, GetStockPrice are Java classes corresponding to request message objects.
DownloadImageResponse, GetStockPriceResponse are Java classes corresponding to response message objects.
package-info is a class describing the package name for the generated classes.
ObjectFactory is a class which acts a factory for schema derived interfaces and classes
StockService is the service endpoint interface
StockPriceImplService is the stub for service implementation.

The advantage of generating these classes is that you get the infrastructure code for consuming a non-Java based SOAP web service. There is no need for the consumer to look at the WSDL to get the service endpoint URL and server namespace.

Source code

You can download the source code at:

wsimport Ant Task in Eclipse admin WebServices
wsimport is a tool shipped with JDK/JRE and is present in the 'bin' folder of JDK/JRE. The purpose of this small utility is to take wsdl file as input and generate JAX-WS artifacts for building the service endpoint infrastructure code. It can also generate the required exception classes by...
<p>wsimport is a tool shipped with <a href="http://www.javaexperience.com/java-difference-between-jvm-jre-jdk-jit/">JDK/JRE</a> and is present in the "bin" folder of JDK/JRE. The purpose of this small utility is to take wsdl file as input and generate JAX-WS artifacts for building the service endpoint infrastructure code. It can also generate the required exception classes by looking at the faults definition present in the wsdl. There are a number of options that can be used with this tool. Let us now go through the steps to use wsimport tool in Eclipse as an ant task. </p> <h2>Project Structure:</h2> The following screenshot shows how the project looks like before we start: <img src="http://www.javaexperience.com/wp-content/uploads/2012/09/wsimport-project-structure.jpg" alt="" title="wsimport project structure" width="244" height="168" class="alignnone size-full wp-image-321" /> <p> A few points to note here: <ul> <li> <strong>wsdl_xsd</strong> folder contains the wsdl and xsd files for the web service. </li> <li> <strong>wsimportclasses</strong> and <strong>wsimportsrc</strong> folders will contain the generated class and java files after we run the ant build tasks. These classes will help in writing the web service <a href="http://www.javaexperience.com/exhaustive-differences-between-abstract-class-and-interface/" title="Differences between Abstract class and interface">end point interface</a> and client code. </li> </ul> </p> <h2>WSDL for web service</h2> The following is the WSDL file from where we start and build up the web service. 1 <h2>XSD for data types</h2> <p>As you would have noticed, the above <a href="http://www.javaexperience.com/jax-ws-rpc-vs-document-style-web-services/" title="RPC vs DOCUMENT style web services">WSDL specifies document/literal</a> for encoding and hence the data types are externalized into an XSD which is listed below:</p> 1 <h2>Ant build.xml task</h2> The build.xml shown in the project structure has the following contents. Here we are invoking the wsimport.exe tool which comes with JDK/JRE. 1 <p>If you want to know the meaning of various arguments used above, then set the <a href="http://www.javaexperience.com/setting-path-classpath-and-java_home-enviornment-variables/" title="Setting PATH, CLASSPATH and JAVA_HOME enviornment variables">JAVA_HOME enviornment variable</a> and issue wsimport command on the console. -s and -d arguments are used to specify the location of generated .java and .class files.</p> <h2>Running the ant task in eclipse</h2> <img src="http://www.javaexperience.com/wp-content/uploads/2012/09/Run-ant-task.jpg" alt="" title="Run ant task" width="571" height="538" class="alignnone size-full wp-image-326" /> Build output on the console is: 1 After running the ant task, refresh the project in eclipse (select the project in project explorer and press F5 key) <h2>Generated files</h2> The following screenshot shows the .java and .class files generated by the wsimport. <img src="http://www.javaexperience.com/wp-content/uploads/2012/09/wsimport-output-files1.jpg" alt="" title="wsimport output files" width="295" height="527" class="alignnone size-full wp-image-325" /> <p> <strong>DownloadImage</strong>, <strong>GetStockPrice </strong>are Java classes corresponding to request message objects. <strong>DownloadImageResponse</strong>, <strong>GetStockPriceResponse </strong>are Java classes corresponding to response message objects. <strong>package-info</strong> is a class describing the package name for the generated classes. <strong>ObjectFactory</strong> is a class which acts a factory for schema derived interfaces and classes <strong>StockService</strong> is the service endpoint interface <strong>StockPriceImplService</strong> is the stub for service implementation. The advantage of generating these classes is that you get the infrastructure code for consuming a non-Java based SOAP web service. There is no need for the consumer to look at the WSDL to get the service endpoint URL and server namespace. </p> <h2>Source code</h2> You can download the source code at: <div class="download-code"><img src="http://www.javaexperience.com/wp-content/uploads/2012/10/downloadcode-e1349278942164.jpg" alt="ANT task for wsimport"><a href="http://www.javaexperience.com/wp-content/uploads/2012/09/Eclipse_wsimport_ant_task.zip"><br><br><strong>wsimport Ant Task Example</strong></a></div>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.