wsimport Ant Task in Eclipse

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:

Copyright © 2013 Java Experience. All rights reserved.