Wednesday, February 8, 2006

Web Services in minutes with Apache Axis

Have you ever worked on a project that has been successfully deployed for some time only to have the topic of integration creep into the picture late in the game? Lucky for us there is an easy and efficient way to deploy Web Services for integration literally within minutes! If you've been faced with such a situation or are simply curious, please read on!

I recently ran into this scenario and can tell you the documentation will take you longer to produce than the actual implementation! We'll be using Apache's Axis project to implement our Web Service.

Apache Axis is an implementation of the SOAP ("Simple Object Access Protocol") submission to W3C.

For this blog entry, we'll assume you have a pre-existing web application with certain functionality you would like to expose. We'll also assume we only need to get a string representation of the current time on the server (yes, very simple... but how many times do you want to see "Hello World!"?)

Let's get started, download the latest version of Apache's Axis project located here (get version 1.3.) Unzip the file you downloaded and copy the jar files from axis-1_3/lib to your application's WEB-INF/lib directory (don't need to copy any libraries you already have of course.) Put the following entries in your application's web.xml file:

[sourcecode language="xml"]


org.apache.axis.transport.http.AxisHTTPSessionListener




AxisServlet

Apache-Axis Servlet


org.apache.axis.transport.http.AxisServlet




AxisServlet
*.jws



wsdl
text/xml


xsd
text/xml
[/sourcecode]Create a file named MyWebService.jws in the root dir of your web application (along side index.jsp or index.html) The contents of this file should be:

[sourcecode language="java"]
import java.util.Date;
public class MyWebService {
public String getTime() {
return new Date().toString();
}
}[/sourcecode]
OK, take a deep breath... your done. Now you obviously want to test this, there is plenty of documentation for Axis to help you write your own unit tests. But in my case, the primary integrating system was written using PHP 5 and planned to use PHP's new soap extension. (Be careful here, PHP 5's soap extension is not compatible with Axis 1.2 only 1.3!)PHP can integrate with this service VERY easily as well, to test I got the latest for PHP 5 and wrote the following:
[sourcecode language="php"]
$serviceWsdl = "http://localhost:8888/" .
"test/MyWebService.jws?wsdl";

try {
$client = new SoapClient($serviceWsdl);
echo "The time returned by the server was: " .
$client->getTime();
} catch(SoapFault $fault) {
echo "there was an issue : \n" .
" faultcode: $fault->faultcode\n" .
" faultstring: $fault->faultstring\n";
}[/sourcecode]
That was it... my PHP script printed the time in "toString" format from my server.Now for the disclaimer, each application will have it's own requirements for integration. Although Axis is a great tool for Web Services, it should not be viewed as the magic hammer for all jobs. There may not even be a need for Web Services written using SOAP as mentioned in a blog entry by Luis Perez.That being said, I believe Apache's Axis project is a great way to get your application out there with Web Services quickly and efficiently.