ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Creating Web Services with Apache Axis

by Dion Almaer

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:

% cp samples\usersguide\example2\Calculator.java %TOMCAT_HOME%\webapps\axis\Calculator.jws

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.

Write a Client That Accesses the Web Service

Now we have a deployed Web service; we need to access it. Let's look at a client that allows us to pass in a math operation (add or subtract) and the two amounts to work with.

package samples.userguide.example2;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;

import javax.xml.rpc.ParameterMode;

public class CalcClient {
       Options options = new Options(args);

       String endpoint = "http://localhost:" + options.getPort() +

// Do argument checking
       args = options.getRemainingArgs();

       if (args == null || args.length != 3) {
           System.err.println("Usage: CalcClient <add|subtract arg1 arg2");

       String method = args[0];
       if (!(method.equals("add") || method.equals("subtract"))) {

// Make the call
       Integer i1 = new Integer(args[1]);
       Integer i2 = new Integer(args[2]);

       Service  service = new Service();
       Call     call    = (Call) service.createCall();

       call.setTargetEndpointAddress(new java.net.URL(endpoint));
       call.setOperationName( method );
       call.addParameter("op1", XMLType.XSD_INT, ParameterMode.PARAM_MODE_IN);
       call.addParameter("op2", XMLType.XSD_INT, ParameterMode.PARAM_MODE_IN);

       Integer ret = (Integer) call.invoke( new Object [] { i1, i2 });

       System.out.println("Got result : " + ret);

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.

Pages: 1, 2

Next Pagearrow