Developing Web Services Contract First Approach

This example will show case on developing a web service with a contract first approach. There are two ways by which web service can be developed, one is the contract last and the other is the Contract First Approach.

This is a very simple Product Catalog web service with Contract First Approach, where user will input the ProductId Number to find out the desired Product Name in the Catalog. To setup and run this example one need to know few things given below:

Ant: Apache Ant is a Java library and command-line tool who’s mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks. We can download the Latest relase of ANT from http://ant.apache.org/bindownload.cgi..

  1. Set <ANT_HOME>bin| to the path of the user variable. (All other Environment setup is similar to Example-I).
  2. Create a file name product.wsdl inside <AXIS_HOME>bin|wsdl| directory as shown below
?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:ns="http://contract.axis2.ibm.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://contract.axis2.ibm.com"> <wsdl:types>

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://contract.axis2.ibm.com"> <xs:element name="getProductName"> <xs:complexType> 
<xs:sequence>
<xs:element minOccurs="0" name="productNumber" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="getProductNameResponse">
 <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" nillable="true" type="xs:string"/> 
</xs:sequence> </xs:complexType> </xs:element> </xs:schema> 
</wsdl:types>
<wsdl:message name="getProductNameRequest"> <wsdl:part name="parameters" element="ns:getProductName"/> </wsdl:message> 

<wsdl:message name="getProductNameResponse"> <wsdl:part name="parameters" element="ns:getProductNameResponse"/> </wsdl:message>
<wsdl:portType name="ProductPortType"> 
<wsdl:operation name="getProductName">
<wsdl:input message="ns:getProductNameRequest" wsaw:Action="urn:getProductName"/>
<wsdl:output message="ns:getProductNameResponse" wsaw:Action="urn:getProductNameResponse"/>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="ProductSoap11Binding" type="ns:ProductPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getProductName">
<soap:operation soapAction="urn:getProductName" style="document"/>
<wsdl:input> <soap:body use="literal"/> </wsdl:input>
<wsdl:output> <soap:body use="literal"/> </wsdl:output>
</wsdl:operation> </wsdl:binding>

<wsdl:binding name="ProductSoap12Binding" type="ns:ProductPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getProductName"> <soap12:operation soapAction="urn:getProductName" style="document"/>
<wsdl:input> <soap12:body use="literal"/>
</wsdl:input> <wsdl:output> <soap12:body use="literal"/>
</wsdl:output> </wsdl:operation> </wsdl:binding>
<wsdl:binding name="ProductHttpBinding" type="ns:ProductPortType">
<http:binding verb="POST"/> <wsdl:operation name="getProductName">
<http:operation location="Product/getProductName"/>
<wsdl:input> <mime:content type="text/xml" part="getProductName"/>
</wsdl:input> <wsdl:output>
<mime:content type="text/xml" part="getProductName"/>
</wsdl:output> 
</wsdl:operation> </wsdl:binding>

<wsdl:service name="Product"> <wsdl:port name="ProductHttpSoap11Endpoint" binding="ns:ProductSoap11Binding"> 
<soap:address location="http://localhost:8080/axis2/services/Product"/> 
</wsdl:port> <wsdl:port name="ProductHttpSoap12Endpoint" binding="ns:ProductSoap12Binding">
<soap12:address location="http://localhost:8080/axis2/services/Product"/> 
</wsdl:port> <wsdl:port name="ProductHttpEndpoint" binding="ns:ProductHttpBinding">
<http:address location="http://localhost:8080/axis2/services/Product"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

 

  1. Using the tool <AXIS2_HOME>|bin|wsdl2java.bat, we will create the Skeleton and other supported files for this service.
  2. Open command Prompt and move to <AXIS2_HOME>|bin| and execute the following command
    wsdl2java.bat –uri <AXIS2_HOME>\bin\wsdl\product.wsdl -d adb -s -ss -sd -ssi -o build\service
  3. This will create an output named service inside <AXIS2_HOME>|bin|build| directory as shown below in Figure 17.

 

The contents of target_directory_name are:

build.xml
resources\services.xml
resources\product.wsdl
src|com|ibm|axis2|contract|ExtensionMapper.java
src|com|ibm|axis2|contract|GetProductName.java
src|com|ibm|axis2|contract|GetProductNameResponse.java
src|com|ibm|axis2|contract|ProductMessageReceiverInOut.java
src|com|ibm|axis2|contract|ProductSkeleton.java
src|com|ibm|axis2|contract|ProductSkeletonInterface.java

In the above list, the Java file named src|com|ibm|axis2|contract|ProductSkeleton.java is the one which is used to place our custom Business logic code.

/** * ProductSkeleton.java * This file was auto-generated from WSDL * by the Apache Axis2 version: 1.5.2 Built on 
: Sep 06, 2010 (09:42:01 CEST) */ 
package com.ibm.axis2.contract; 
/** * ProductSkeleton java skeleton for the axisService */ 
public class ProductSkeleton implements ProductSkeletonInterface{ 
/** * Auto generated method signature * @param getProductName0 */ 
public com.ibm.axis2.contract.GetProductNameResponse getProductName
 (com.ibm.axis2.contract.GetProductName getProductName) {
throw new java.lang.UnsupportedOperationException(
"Please implement " + this.getClass().getName() + "#getProductName");
}
}

Let us add our Business logic in the above class to implement our functionality.

Listing 7. Business Logic added in the ProductSkeleton.java

/** * ProductSkeleton.java * This file was auto-generated from WSDL *
by the Apache Axis2 version: 1.5.2 Built on : Sep 06, 2010 (09:42:01 CEST) */
package com.ibm.axis2.contract;
import com.ibm.axis2.contract.*;
import java.util.*; 
import org.apache.axis2.engine.AxisError; 

/** * ProductSkeleton java skeleton for the axisService */ 

public class ProductSkeleton implements ProductSkeletonInterface{ 
private static HashMap<Integer, String> productMap = new HashMap<Integer, String>();
static { productMap.put(1, "Colgate"); 
productMap.put(2, "Pepsodent"); 
productMap.put(3, "Neem");
}
/** * Auto generated method signature * @param getProductName0 */

public com.ibm.axis2.contract.GetProductNameResponse getProductName (
com.ibm.axis2.contract.GetProductName getProductName) {

/** Custom Logic - Starts here */

int inputProductNumber = getProductName.getProductNumber();
String productName = productMap.get(inputProductNumber);
GetProductNameResponse response = new GetProductNameResponse();
if (productName != null) { response.set_return(productName);
} else {
throw new AxisError("Product Record Not Found");
}
return response;
/* Custom Logic - Ends here */
} }

 

  1. Now, its time to build and test our web service implementation.
  2. Open command promt and move to <AXIS2_HOME>bin|build|service| and type the following command as shown below.Ant jar.server

Figure 18. Build and Test with ANT

The Above ANT build will create Product.aar file inside <AXIS2_HOME>bin|build|service|build|lib| as shown below
Figure 19. ANT build directory structure and Product.aar file

The Steps for Deploying the Product.aar file is similar to CreditCardService deployment on tomcat.

  1. Start tomcat and open browser by typing http://localhost:8080/axis2 and click on Administration. (You need to provide the default username: admin and password: axis2 to login).
  2. After sucessfully logging, click on Upload web service and the browse the Product.aar file in the <AXIS2_HOME>bin|build|service|build|lib| directory and finally click on Upload(to sucessfully deploy the web service).
  3. Now click on Available Service or paste this link http://localhost:8080/axis2/axis2-admin/listService in the browser to find out the deployed Product Service as shown below.

You can check the WSDL of this Product web service at http://localhost:8080/axis2/services/Product?wsdl