Creating Web Services with Apache Axis

Web services have been a buzzword for a while. A friend used to say "Web services are like high school sex. Everyone is talking about doing it, but hardly anyone is, and those that are probably aren't doing it well." These days, though, Web services are moving to college, so to speak, and lots of people are starting to "do it" more often and better than before. Tools are maturing, and creating and working with Web services isn't all that hard anymore.

IBM has given a lot of code to the Apache group, including SOAP4J, their group of SOAP tools. The Apache SOAP and SOAP4J guys got together and are working on the latest and greatest tool set called Apache AXIS, which features not only better performance, but also some new features that make it trivial to play in this new world. I see the most common actions being "I want to expose this functionality as a Web service," and "I want to access that Web service." Surely it should be very straight-forward to strap on this interface, and you shouldn't have to learn everything there is to know about the underlying platform. This is the same idea as not having to know about the IP and TCP layer when accessing a URL over HTTP. Let's keep it simple, folks.

Notes on running sample application

You can download the code and scripts to run the Fibonacci Web service. Your first step is to get Apache Axis running; then you can unzip the code and follow our steps. Read the README.txt in the distribution, as it covers how to set up your CLASSPATH and other deployment issues.

In this article, I will show two parts of this new system:

First, I will show the "easy to deploy" feature that lets you drop a source file into the AXIS Web application and have it become a Web service -- just like that!

Then we will use the new WSDL2Java and Java2WSDL tools to see how we can quickly get a WSDL descriptor and access the associated Web service, and then how to easily expose some Java code.

All of the code that is listed (and downloadable) was written for Apache Axis beta1. There are more instructions on running the code at the end of the article.

NOTE: I assume that you have basic knowledge of Web services. If you need to look up what a WSDL file is, checkout the Web services section of ONJava.com.

Deploying Your Code as a Web Service in One Easy Step

The Apache guys realized that it would be really nice to be able to drop some code somewhere and have it become a Web service "just like that." This simplicity is a current trend; Microsoft has it in .NET, and BEA in the WebLogic 7 platform. But just how easy is it to:

Deploy a piece of code?

Write a client that accesses the Web service?

Obtain the WSDL for the deployed Web service?

Deploy a Java Class as a Web service

Let's take the simple Calculator.java class from the samples.userguide.example2 package and expose its two methods (add() and subtract()) through Web services. We simply copy the Java file into the Axis Web application, using the extension .jws instead of .java:

Just by having the code (with the .jws extension) in the Web application deploys it and allows us to access it. If we open a browser and access the file (e.g. http://localhost:8080/axis/Calculator.jws) we will be told that we are talking to a Web service. How easy was that?! A simple copy command and we are done.

The code first imports all of the required classes. Then we set the URL of the Web service that we want to invoke. Skip past the argument checking, and we get to the meat: we configure the method that we want to call, the parameters to pass, and then invoke the service itself. So, we have deployed and accessed the Web service by writing a minimal amount of code.