Webservices using Axis

Posted by Matt Osentoski Fri, 23 Feb 2007 14:56:00 GMT

This article is a howto that describes setting up a web service using the 'Bottom Up' approach.

Intro

The first step to building web services is to figure out which method to start with. (Top down or Bottom up)

Bottom Up Development

Creating a web service from a Java Bean. This method creates a WSDL file automatically from Java code. To get this type of system to work, you will have to create a WSDD file which describes which java objects should be exposed as webservices. This approach is easier in the sense that you don't have to create a WSDL file, however the downside is that there is a greater risk that the WSDL file generated will not work with other systems. I would recommend trying Bottom Up development first. You can switch to Top Down development if you run into severe interoperability problems.

Top Down Development

This approach starts with the WSDL file. Java files are then generated from WSDL using a utility (WSDL2Java). This method has the highest chance of interoperability, but requires that you create a WSDL file from scratch, which can be very difficult and error prone process without the help of tools.

Bottom Up Development (Using JWS files)

Create a web service from a Java Bean that is renamed with the .jws extension This method creates a web service from a Java bean without the need to create a WSDD or WSDL file. It is the easiest approach to use, but also the most limiting in terms of functionality and interoperability. This method should NOT be used!!

Development (Bottom Up) - Server

Assuming you've chosen to develop using the Bottom Up approach, begin by creating your Java Objects. I generally create three layers:
1) Business Object Layer. This layer consist of the Beans that make up my model
2) DAO layer for database persistance. (Using Hibernate or JDBC)
3) Service Layer. This is the loosely grained layer used for making the web service calls (Ex: getCustomerByName(String name)). This is also the layer that will generate the WSDL file(s).

(NOTE: Save yourself a lot of pain and write some tests. There's nothing worse than trying to resolve an obscure Hibernate error while end-to-end testing your services)

Now that your Java code has been written (and hopefully tested ;) ) you can begin to write your WSDD file. The purpose of the WSDD file is to describe which java class will be exposed using web services. See this WSDD file as an example. The most important part is the comment section, I included, called 'Put your service definitions here'. Follow the example to add your own classes. If you're deploying your web services as a web application, the WSDD file should be called 'server-config.wsdd' and placed under your /WEB-INF directory. The Axis servlet will look for this file on startup. (This also saves you the annoyance of using the Axis admin tools for service startup.)

Now you will need to add the following Axis 1.2 library dependencies under /WEB-INF/lib:
axis.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
log4j-1.2.8.jar (your version may vary)
saaj.jar
wsdl-4j-1.5.1.jar (again.. your version may vary)

All of these libraries are included with Axis.

Next, you will have to modify your web.xml file to startup the Axis servlet and services. See this web.xml file as an example.

At this point you can compile all of your Java code into /WEB-INF/lib (as a jar) or /WEB-INF/classes and deploy the War file. Your web services should now be running.
To check, try accessing your services from a web browser:
http://localhost:8080/yourAppName/services/SomeService?wsdl

(Note: 'YourAppName' is the name of your application context. 'SomeService' will be the name of your service as defined in the WSDD file.)

This will display the WSDL file that is dynamically created. To call a method use:
http://localhost:8080/yourAppName/services/SomeService?method=aMethodThatDoesCoolStuff

(NOTE: 'aMethodThatDoesCoolStuff' would be the name of a method in your service layer. You can also set method params by adding paramName=paramValue pairs at the end of the URL)


Development - Client

The client code is automatically created using a utility called WSDL2Java. See this wsdl_run.bat script for an example that creates client code. A simple example for creating client stubs is also below:
java -cp %CLASS_PATH% org.apache.axis.wsdl.WSDL2Java -o .\src -d Session -p test.ws http://localhost:8080/myapp/services/MyService?wsdl

This command will create all the model objects and SOAP bindings. There will be a class called MyServiceSoapBindingStub that you will instantiate using another class called MyServiceImplServiceLocator to make web service calls. Compile all the generated code then call the web service using the example below.

Example:
MyServiceImplServiceLocator locator = new MyServiceImplServiceLocator();
MyServiceSoapBindingStub soapCall = (MyServiceSoapBindingStub) locator.getMyService();
String coolText = soapCall.aMethodThatDoesSomethingCool();
System.out.println("Output from WS: " + coolText + "\n\nYes! It works!!");

Posted in  | Tags , ,  | no comments