Labels

Monday, June 18, 2012

How to invoke a Web-Service to write java code?

Step 1 – Get Apache Axis2
Download Apache Axis (http://ws.apache.org/axis2/). Just unzip it in a dir. Lets say your Apache Axis is unzipped here: c:\axis2. Lets call this dir <AXIS_HOME>
Step 2 – Get hold of the wsdl
Get hold of the wsdl file that describes the web-service. Every web-service has an associated wsdl file. Normally, the web-service URL with a ‘?wsdl’ suffix appended to it, will show you the wsdl file. So, if a web-service you want to invoke is available at: http://host:port/superservice/servicemall . You can see the wsdl via a URL: http://host:port/superservice/servicemall?wsdl
Step 3 – Create the Stub
Stub is nothing but the web-service invocation code that you are going to generate now. Go to your command line window, change to dir: <AXIS_HOME>\bin , and run this command:
WSDL2Java -uri servicemall.wsdl
NOTE: servicemall.wsdl should be present in AXIS_HOME\bin dir. Instead of pointing to that file, you could even use a URL format pointing to the wsdl as follows: WSDL2Java -uri http://host:port/superservice/servicemall?wsdl
You will see that stub code has been generated here: <AXIS_HOME>\bin\src. Here you will see that a file ends with *Stub.java name, this is the stub file you need. The contents of this java file look quite complex, but you dont need to worry about all its contents. You will only need to know the Constructor, Parameter, and Response related sub-classes in this file.
Step 4 – Use the Stub
Lets write a MyWebServiceInvoker.java file now, that will invoke the Stub (which in turn invokes the actual web-service). Assuming the name of the name of the stub file generated was: ServiceMallStub, and that the service we want to invoke takes one param (item id), and returns item details when its method getItemDetails is invoked.
You will notice that parameter and the response are also inner classes in the generated stub java file, and they have appropriate methods for your use.
public class MyWebServiceInvoker
{
 public static void main(String[] args)
 {
  try
  {
   com.servicemall.ServiceMallStub serviceMallStub = new com.servicemall.ServiceMallStub();  
   com.servicemall.ServiceMallStub.MyItem theParam = new com.servicemall.ServiceMallStub.MyItem();
   theParam.setItemid("11");
   theParam.MyItemResponse theResp = serviceMallStub.getItemDetails(theParam);
   boolean result = theResp.getItemResult();
   System.out.println("Result is:" + result);   
  }
  catch (Throwable t)
  {
   System.out.println(t);
  }
 }
Step 5 – Run
Just compile and run the MyWebServiceInvoker. To compile, you must put the <AXIS_HOME>\lib\ jar files in the classpath. To run in addition you must have the compiled stub classes in your classpath too. (You could also use the build.xml that is generated along with source code for compilation)
Step 6 – Does it work?
If you dont see any errors, and get a response back from the service, all’s well!
A common cause of exception is HTTP Protocol mismatch (although the exception message doesnt give suitable hint about it!). You may have to ask your stub to use HTTP 1.0 protocol, instead of the default HTTP 1.1 protocol it uses.  This you can do in the constructor class of the stub.
Search for this line: //Set the soap version
_serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
Now, just below this line, add this line:
_serviceClient.getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.HTTP_PROTOCOL_VERSION, org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10);
Compile, and try again.

No comments:

Post a Comment