August 04, 2010

Consuming web services using axis2 with example


Consuming web services using axis2 with example
Consuming web services using axis2 tutorial
Using axis2 engine tutorial
Using axis2 web services in command prompt

Here in this tutorial you will find how to consume a web service using axis2 engine.
This tutorial also includes the comparison of codes and explanations between axis1.4 and axis2 ( axis2-1.5)

WSDL – Web Service Description language.

  WSDL used in this tutorial
You can find many free and paid web services ( including gmail web services,etc) in the site
               
Axis2
                Download the axis2 version 1.5 from the apache official website
2.      In Standard Binary Distribution, select Zip and save the file

Consuming web service
                In command prompt direct to the path          …\axis2-1.5.1\bin>
                Create java  classes form the wsdl file by entering
bin> wsdl2java.bat –uri  http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl –p com.myproject –d adb –s
                Once you hit enter the java files will be extracted to the package you specified (in this case com/myproject).
                -d specifies the data binding
               
Create a Class ( say Test.java) to test the web service.
Test.java

 package com.myproject;  
 public class Test {  
    public static void main( String args[] )  
    {  
       try  
       {  
       WeatherStub weatherStub = new WeatherStub( "http://ws.cdyne.com/WeatherWS/Weather.asmx" );  
       WeatherStub.GetCityWeatherByZIP wCityWeatherByZIP = new WeatherStub.GetCityWeatherByZIP();  
       wCityWeatherByZIP.setZIP("95712");  
       WeatherStub.GetCityWeatherByZIPResponse wCityWeatherByZIPResponse;  
       wCityWeatherByZIPResponse = weatherStub.getCityWeatherByZIP( wCityWeatherByZIP );  
       System.out.println( "ZIP Used : " + wCityWeatherByZIP.getZIP() );  
       WeatherStub.WeatherReturn weatherReturn = wCityWeatherByZIPResponse.getGetCityWeatherByZIPResult();  
       // print visibility  
          System.out.println( "Visibility:" + weatherReturn.getVisibility() );  
       // print state  
          System.out.println( "State:" + weatherReturn.getState() );  
       // print pressure  
          System.out.println( "Pressure:" + weatherReturn.getPressure() );  
       // print wind  
          System.out.println( "wind:" + weatherReturn.getWind() );  
       // print weather station city  
          System.out.println( "weather station city:" + weatherReturn.getWeatherStationCity() );  
       // print city  
          System.out.println( "city:" + weatherReturn.getCity() );  
       }  
       catch( Exception e ) { e.printStackTrace(); }  
    }  
 }  

Just compile the entire package
>javac com\myproject\*.java

If you are unable to compile the n the issue will be in setting the correct class path.
Set all the  needed jars to the class path.
For eg. Im having all my jars  in the location D:\AXIS2JARS. So I did like this,
>set classpath= D:\AXIS2JARS\wsdl4j-1.6.2.jar;D:\AXIS2JARS\wstx-asl-3.2.4.jar;D:\AXIS2JARS\wstx-LICENSE.txt;D:\AXIS2JARS\xalan-2.7.0.jar;D:\AXIS2JARS\xercesImpl-2.6.2.jar;
Its better to set all the axis jars ( found in axis2 lib folder "standard binary editon" that you have downloaded ) in class path to save time.


And run the Test Class
>java com.myproject.Test

You will find the output as below.
ZIP Used : 95712
Visibility:
State:CA
Pressure:29.87F
wind:SW8
weather station city:Cool
city:Chicago Park

1 comment :