Regarding my last tutorial XML Web service – Axis2 client & xml binding, Webservice wsdl was generated by IBM Rational App Developer (RAD 6.0),
so the schema generated for complex types may differ from other openSourced tools. (e.g. HashMap, Student schemas).
This time, I'll show simple webservice passing a complex type as i/o parameter (here:
java.util.Vector), developed using NetBeans5.5.1 IDE, and their prespective Axis2 XmlBeans generated clients:
- Create new web Application, (if using Tomcat uncheck set source level to J2EE 1.4 when creating the application, to support JAX-WS in webservice development)
- Create new Web service (using JAX-WS APIs), like this:
package test;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService()
public class ComplexService {
** /
* Web service operation
*/
@WebMethod
public Vector getVector(@WebParam(name = "name") String name) {
Vector vec=new Vector();
vec.add(name);
vec.add(new Date().getDay());
vec.add(new Date().getTime());
return vec;
}
**/
* Web service operation
*/
@WebMethod
public String setVector(@WebParam(name = "vectorObj") Vector vectorObj) {
// TODO implement operation
String strVal="";
for(int i=0; i<vectorObj.size();i++)
{
strVal+=vectorObj.get(i)+";";
}
return strVal;
}
}
- Generate Axis2 client, using XmlBeans, using the commad line:
wsdl2java -uri http://localhost:8080/service/ComplexServiceService?wsdl -o C:\client -d xmlbeans
- Generate client jars using the commands:
--> cd C:\client
--> ant
- Create your client application, including the following jars in classpath:
"C:\client\build\lib\ComplexServiceService-test-client.jar"
"Axis2-Binary-Installation-dir\lib\*.jar"
- Here's sample client:
import java.util.Date;
import org.apache.xmlbeans.XmlDate;
import org.apache.xmlbeans.XmlLong;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlString;
import test.GetVectorResponseDocument;
// your class definition
public static void main(String[] args) {
try {
// initiate client stub
test.ComplexServiceServiceStub stub = new
// invoke getVector method
test.GetVectorDocument
test.GetVector doGetVector = getDoc.addNewGetVector();
doGetVector.setName("tempName");
GetVectorResponseDocument vres= stub.getVector(getDoc);
XmlObject[] vobjs = vres.getGetVectorResponse().getReturnArray();
// retrieve vector objects
for(int i=0;i<vobjs.length;i++)
{
XmlObject tempObj=vobjs[i];
// tempObj may be instance of any org.apache.xmlbeans.impl.values.*
if(tempObj instanceof
{
org.apache.xmlbeans.impl.values.XmlStringImpl
String strVal = tempVal.stringValue();
System.out.println(strVal);
}
else if(tempObj instanceof
{
org.apache.xmlbeans.impl.values.XmlIntImpl tempVal=
(org.apache.xmlbeans.impl.values.XmlIntImpl)tempObj;
int intVal=tempVal.intValue();
System.out.println(intVal);
}
else if(tempObj instanceof
{
org.apache.xmlbeans.impl.values.XmlLongImpl
long longVal = tempVal.longValue();
System.out.println(longVal);
}
}
// invoke setVector(Vector) method
test.SetVectorDocument
test.SetVector doSetVector = setDoc.addNewSetVector();
Date date=new Date();
long id=date.getTime();
// 1st Item in vector
XmlString strItem = XmlString.Factory.newInstance();
strItem.setStringValue("name");
// 2nd Item in vector
XmlDate dateItem = XmlDate.Factory.newInstance();
dateItem.setDateValue(date);
// 3rd Item in vector
XmlLong idItem=XmlLong.Factory.newInstance();
idItem.setLongValue(id);
//set vector objects
XmlObject[] setobjs=new XmlObject[3];
setobjs[0]=strItem;
setobjs[1]=dateItem;
setobjs[2]=idItem;
// or XmlObject[] setobjs={strItem,dateItem,idItem};
doSetVector.setVectorObjArray(setobjs);
test.SetVectorResponseDocument s_resp = stub.setVector(setDoc);
String result = s_resp.getSetVectorResponse().getReturn();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
Note:
NetBeans generated wsdl looks like this:
<?xml version="1.0" encoding="UTF-8"?><definitions
xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://test/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://test/"
name="ComplexServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://test/"
schemaLocation="http://localhost:8080/service/ComplexServiceService/__container$publishing$s
ubctx/WEB-INF/wsdl/ComplexServiceService_schema1.xsd"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"/>
</xsd:schema>
</types>
<message name="getVector">
<part name="parameters" element="tns:getVector"/>
</message>
<message name="getVectorResponse">
<part name="parameters" element="tns:getVectorResponse"/>
</message>
<message name="testTable">
<part name="parameters" element="tns:testTable"/>
</message>
<message name="testTableResponse">
<part name="parameters" element="tns:testTableResponse"/>
</message>
<message name="setVector">
<part name="parameters" element="tns:setVector"/>
</message>
<message name="setVectorResponse">
<part name="parameters" element="tns:setVectorResponse"/>
</message>
<message name="getMap">
<part name="parameters" element="tns:getMap"/>
</message>
<message name="getMapResponse">
<part name="parameters" element="tns:getMapResponse"/>
</message>
<portType name="ComplexService">
<operation name="getVector">
<input message="tns:getVector"/>
<output message="tns:getVectorResponse"/>
</operation>
<operation name="testTable">
<input message="tns:testTable"/>
<output message="tns:testTableResponse"/>
</operation>
<operation name="setVector">
<input message="tns:setVector"/>
<output message="tns:setVectorResponse"/>
</operation>
<operation name="getMap">
<input message="tns:getMap"/>
<output message="tns:getMapResponse"/>
</operation>
</portType>
<binding name="ComplexServicePortBinding" type="tns:ComplexService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="getVector">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="testTable">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="setVector">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
<operation name="getMap">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="ComplexServiceService">
<port name="ComplexServicePort" binding="tns:ComplexServicePortBinding">
<soap:address location="http://localhost:8080/service/ComplexServiceService"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"/>
</port>
</service>
</definitions>
Useful link:
No comments:
Post a Comment