Wednesday, July 20, 2011

Developing web services using Apache Axis2

I am going to present to all folks who are eager to develop web services using Apache Axis2. I am assuming that you guys have fair idea about what web service is, SOAP, structure of a valid WSDL, and all the basics about web services.

What I am about to tell you is if you have a valid WSDL document(xml) then it's quite easy to develop a web service out of it in simple steps. This is called the top down approach. Apache Axis2 distribution provides set commands that generates Java code when WSDL is given as input. The generated code is just a skeleton of the web service wherein you have fill it with your business logic.

Requirements:
  • Eclipse IDE 3.4 or above
  • Apache Axis2 (stand alone) 1.4 or above: Download releases
  • A WSDL ( I am using CalcWSDLFile.wsdl)
In the axis distribution folder: ${AXIS2_HOME}/bin, you will find two script files: WSDL2Java.bat & Java2wsdl.bat. I will be using wsdl2Java.bat for since I am working on Windows and also because it's a top down approach. If you are working under Linux, there will be respective .sh files accordingly. 
My WSDL file: CalcWSDLFile.wsdl

This script (wsdl2java.bat) uses few options to generate code accordingly. Now, we are all set. Along with this, these steps will allow you to setup your development environment correctly.

1. Create a folder with name CalculatorWS. I have created it under C:\Amit\webservice\. This will be your workspace (project) folder in Eclipse. 

2. To generate server side code: Execute the below line:
wsdl2java.bat  -ss -g -u -sd -ssi -d adb -uri  C:\Amit\webservice\CalculatorWS\wsdl\CalcWSDLFile.wsdl  -o C:\Amit\webservice\CalculatorWS
-o: The path where the code is generated. The absolute path of folder: CalculatorWS
-uri: The path of of your WDSL file
The command should execute without throwing any error. To know more about rest of the script options mentioned above, have a look at the reference

3. Copy the lib folder under ${AXIS2_HOME} to C:\Amit\webservice\CalculatorWS\
i.e. the absolute path of lib will look like: C:\Amit\webservice\CalculatorWS\lib

4. Open Eclipse. File -> New -> Java Project. Now, provide project name. In the Contents frame, select “Create project from existing source” and provide the path of folder: C:\Amit\webservice\CalculatorWSClick Finish. Your Eclipse work space is now setup successfully.
Look of my workspace: CalculatorWS

5. Now, next thing is to fill the generated with business logic. As highlighted in the screenshot, there will be a source file with the name: {service-name}Skeleton.java. {service-name} is got from the WSDL. This is the file where you come in, process the request, do some logic and reply with a response. 

Deployment
Once you are done with your development, it's time for deployment.

1. Create a folder named CalculatorWS under ${AXIS2_HOME}\repository\services. Then create a folder with name under META-INF under ${AXIS2_HOME}\repository\services\CalculatorWS

2. Copy all the files under C:\Amit\webservice\CalculatorWS\resources to ${AXIS2_HOME}\repository\services\CalculatorWS\META-INF. This folder contains XSDs, WSDLs and other web service related XMLs.

3. Copy all the compiled class files along with packages from work space folder: C:\Amit\webservice\CalculatorWS\bin to ${AXIS2_HOME}\repository\services\CalculatorWS.

4. Your web service is now successfully deployed. Open command prompt, goto ${AXIS2_HOME}\bin

5. Run the command axis2server.bat. You will see that your service is being deployed successfully. From the browser, navigate to URL: http://{your-host-name}:{axis2-server-port-number}/axis2/services/. For example, in my case: http://localhost:8090/axis2/services/
Web service getting deployed successfully
A list of deployed web services is displayed. Click the hyper link of your web service, the WSDL is displayed. Look for the tag soap:address. The value of the attribute location is the URL for the clients to connect to this web service. SOAPUI is a good client to test your web service.

I hope people will find this post useful.

2 comments:

  1. Web service client stub can also be generated in a similar way using the command:

    wsdl2java.bat -a -s -d adb -uri C:\Amit\webservice\CalculatorWS\wsdl\CalcWSDLFile.wsdl -o C:\Amit\webservice\CalculatorWSClient

    This is required when you already have a web service running and you want to connect to it, make a request and process the response.

    ReplyDelete
  2. For deployment, here is an easier way. When server code is generated, you will also find ANT build file is also created: build.xml. Run ANT on this file, an archive file will be created with extension .aar. The file name format will be {service-name}.aar. Copy this archive file to ${AXIS2_HOME}/repository/services directory. The deployment is complete.

    ReplyDelete