Apache Axis2 provides several XML solutions to make Web services development easier and more powerful. Axis2 is an excellent choice for implementing Web services, and the Apache Axis2 and Apache Geronimo duo, both free and open source, make them a viable option to consider.
In the previous part of this series, you were introduced to the Java classes used in this two-part series (see the Resources). You exposed the classes as Web services through WSDL, and then made a JiBX definition description that will communicate with the JiBX data bindings in this article in order to test a Web service. In this test process, you'll compile the JiBX binding classes to create wrapper classes, making your data bindings classes a functional part of the overall Web service.
Now before you move on, make sure all of the JARs in <axis2_home>/lib (including the JiBX ones you copied over in the first part of this series) are in your classpath, and remain so for the duration of this article. Also, make sure you have installed Ant on your system. You can download Ant from Apache (see the Resources).
Let's move right into generating the service.
First, you need to have Axis2 generate the service so that you can later test it with a client that you'll also create. From the start of the last article, you should already have the necessary tools downloaded, and your AXIS2_HOME environment variable set. To begin and create the service, type the instructions in Listing 1.
Listing 1. Creating the service
java org.apache.axis2.wsdl.WSDL2Java -uri C:\apps\webapps\IBM-JiBX\SimpleService.wsdl -p com.ibm.devWorks.xml.simpleService -d jibx -s -ss -sd -ssi -Ebindingfile ../binding.xml |
That should generate the bare bones service for you! Here are some notes on the various switches used:
- The
-uriswitch tells Axis2 where to find the WSDL file that you want to generate a service from. - The
-pswitch tells Axis2 the Java package class to use for the service. - The
-d jibxoption specifies JiBX data bindings. - The
-sswitch specifies that only the synchronous (blocking) methods will be available. - The
-ssswitch tells Axis2 to build the server side code. - The
-sdswitch tells Axis2 to build a service descriptor (services.xml file). - The
-ssiswitch is optional, and simply creates a Java interface for the service implementation (SimpleServiceSkeleton.java). - The
-Ebindingswitch tells Axis2 where to find the JiBX definition description file.
Now you have a Web service, but nothing is defined yet, so you'll define it in the next section.
Methods of the service skeleton get called whenever an operation of the Web service gets successfully called. The implementation code for the actions of the Web service goes in this file, called SimpleServiceSkeleton.java (in this case). You'll find it empty in the src/com/ibm/devWorks/xml/simpleService directory. Define it, as shown in Listing 2.
Listing 2. Defining the service skeleton
package com.ibm.devWorks.xml.simpleService;
public class SimpleServiceSkeleton implements SimpleServiceSkeletonInterface{
public void OneWay
(
com.ibm.devWorks.xml.simpleService.OneWayRequest OneWayRequest
)
{
System.err.println("Request data: " +
OneWayRequest.getRequestData());
}
public com.ibm.devWorks.xml.simpleService.TwoWayResponse TwoWay
(
com.ibm.devWorks.xml.simpleService.TwoWayRequest TwoWayRequest
)
{
System.err.println("Echo String : " +
TwoWayRequest.getEchoString());
System.err.println("Booolean : " +
TwoWayRequest.getBooolean());
TwoWayResponse res = new TwoWayResponse();
res.setEchoString(TwoWayRequest.getEchoString());
res.setInvertedBoolean(!TwoWayRequest.getBooolean());
return res;
}
}
|
The code in Listing 2 has two methods, one for each operation that was discussed in the previous article of this two-part series (see Resources. The first method, OneWay, takes in OneWayRequest Java object as a parameter and returns nothing, since it is a one-way or in only operation. Your implementation just displays to the screen what was sent from the client.
The second method, TwoWay, gets a TwoWayRequest Java object, and returns TwoWayResponse as a response. First the method displays the echoString and booolean fields of the incoming request object, sets the echoString of the response to the value of the echoString field in the request, and sets the value of invertedBoolean in the response object to the inverted value of booolean in the request object (false becomes true and true becomes false). The response object is then returned to the client.
The service is now defined. Next you will build the service and create the JiBX data bindings wrapper classes.
Finalizing for JiBX data bindings
Before you can compile and create the JiBX data bindings wrapper classes, you must first compile the classes themselves. To compile the project, type: ant.
Now switch to the build/classes directory to create the wrapper classes, and type: java -jar <axis2_home>\lib\jibx-bind.jar ..\..\..\binding.xml.
You should notice four new JiBX_* class files. You've created the wrapper classes. Now to repackage the Axis2 service archive, type (in the same directory as before): ant.
Note that you don't actually build the code twice. This time around Ant knows that the code is already built, but it will see the new files and repackage the Axis2 archive file for you.
The service is ready for deployment on Geronimo, which just happens to be next.
Now you'll deploy the service you just created and compiled. If Geronimo isn't currently running, start it by typing: java -jar <geronimo_home>/bin/server.jar
Now copy the service archive file, build/lib/SimpleService.aar, to (note that the exact directory will differ) <geronimo_home>/repository/default/axis2/1166439304031/axis2-1166439304031.war/WEB-INF/services.
The service will soon auto-deploy. Now you're ready to test it with a client which you'll create next.
A client will allow you to test the functionality and proper deployment of your Web service. You'll create one, also with the JiBX data bindings, to test your deployed Web service.
To generate the Web service, type the information in Listing 3.
Listing 3. Generate the Web service
java org.apache.axis2.wsdl.WSDL2Java -uri C:\apps\webapps\IBM-JiBX\SimpleService.wsdl -p com.ibm.devWorks.xml.simpleService -d jibx -Ebindingfile ../binding.xml -s |
The above command simply creates the client stub code. See Generating the service for an explanation of the switches used. Next, you'll define the actual client code you'll use to test the Web service.
Now you'll write the code to test that your Web service deployed successfully by using the client stubs you just created with the JiBX data bindings. Create a src/com/ibm/devWorks/xml/simpleService/Client.java file, and define it as shown in Listing 4.
Listing 4. Testing the client stub and the deployed Web service
package com.ibm.devWorks.xml.simpleService;
public class Client{
public static void main(java.lang.String args[]){
try{
SimpleServiceStub stub =
new SimpleServiceStub
("http://localhost:8080/axis2/services/SimpleService");
oneWay(stub);
twoWay(stub);
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void oneWay(SimpleServiceStub stub){
try{
OneWayRequest req = new OneWayRequest();
req.setRequestData("Here is your requested data!");
stub.OneWay(req);
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
public static void twoWay(SimpleServiceStub stub){
try{
TwoWayRequest req = new TwoWayRequest();
req.setEchoString("echo! ... echo!");
req.setBooolean(false);
TwoWayResponse res = stub.TwoWay(req);
System.out.println("Echo String : " +
res.getEchoString());
System.out.println("Inv Boolean : " +
res.getInvertedBoolean());
} catch(Exception e){
e.printStackTrace();
System.out.println("\n\n\n");
}
}
}
|
The notable code is in bold font. The first thing this client does is initialize the client stub object, which it then passes to both available methods. The first, oneWay, creates a OneWayRequest object, initializing the requestData field with Here is your requested data! as the value. The data then gets sent off to the server, without being returned, since it's a one way operation.
The second method, twoWay, creates a TwoWayRequest object, and initializes the echoString and booolean fields. The request object is then sent to the service, that then returns a TwoWayResponse object. The value of echoString in the response object should, according to the logic you defined in Listing 1, match the value of echoString in the request object, and invertedBoolean in the response object should be the inverted value of the booolean field in the request object, or true in this case.
You've completed the client definition. Now you just need to compile the JiBX binding wrapper classes, as you did for the service.
Finalizing for JiBX data bindings
Before you can compile and create the JiBX data bindings wrapper classes for the client, you first need to compile the classes themselves. To compile the project, type: ant.
Now switch to the build/classes directory to create the wrapper classes, and type: java -jar <axis2_home>\lib\jibx-bind.jar ..\..\..\binding.xml.
You should notice four new JiBX_* class files created, just as you did for the service. To repackage the JAR file containing the client code, type (in the same directory as before): ant.
That's all there is to it! The only item that's left is to test everything out.
Now, run the client code that you just defined, and you should see the correct output on your screen. Before running the client, make sure the JAR containing the client code (build/lib/SimpleService-test-client.jar) is in your classpath.
To run the client, type the following: java com.ibm.devWorks.xml.simpleService.Client.
You should see the output from the service, as shown in Figure 1.
Figure 1. Displaying the standard output results from the service
Notice that the correct values were sent to the service. Now you'll verify that the same correct values make it back to the client by looking at the client output, shown in Figure 2.
Figure 2. Displaying the output of the client
Everything is working like a charm. You're ready to perform JiBX magic!
You should now be an JiBX-Axis2 Web service developer, capable of turning several of your favorite Java classes into Web services using Axis2 and JiBX.
In this two-part article series you exposed Java classes through WSDL, mapped them to XML data using a JiBX definition description. In this article, you created a live Web service that you deployed using Axis2 and Geronimo. To test the final implementation, you created and ran a client using Axis2. And for both the client and service, you specified JiBX data bindings and successfully created the data bindings wrapper classes for each.
| Description | Name | Size | Download method |
|---|---|---|---|
| Part 2 sample code | x-jibx2-source.zip | 24KB | HTTP |
Information about download methods
Learn
- Transform Java classes into Web services using Axis2 and JiBX, Part 1: Use XML to define a Web service from your Java classes (Tyler Anderson, developerWorks, March 2007): In Part 1 of this series, use existing Java classes, and define the Web service using WSDL and the JiBX definition description.
- JiBX home page
Learn more about the JiBX framework for binding XML data to Java objects at a SourceForge project.
- XML and Java technologies: Data binding, Part 3: JiBX architecture (Dennis Sosnoski, developerWorks, April 2003):
Learn more about the JiBX architecture in this article and how to use its great performance and extreme flexibility for mapping between XML and Java objects.
- Apache Axis2 home page: Learn how Axis2 and JiBX have been integrated in Axis2 1.1.
- JiBX: Find more information on JiBX on the Axis2 home page.
- Axis2:
Learn about, join in discussion, download, and contribute at the Axis2 home page.
- Web services and Axis2 architecture (Eran Chinthaka, developerWorks, November 2006): Learn more about the Axis2 architecture and Web services. See why, with its modular and extensible nature, it's becoming the next-generation Web services platform.
- developerWorks XML zone: Learn all about XML at the developerWorks XML zone.
- IBM XML certification: Find out how you can become an IBM-Certified Developer in XML and related technologies.
- XML technical library: See the developerWorks XML zone for a wide range of technical articles and tips, tutorials, standards, and IBM Redbooks.
- developerWorks technical events and webcasts: Stay current with technology in these sessions.
Get products and technologies
- ant: Download this open source, Java-based build tool that uses XML-based configuration files.
- Apache Axis2: Download this core engine for Web services.
- JiBX 1.1: Download the JiBX framework for XML data binding to Java.
- Geronimo 1.1.1: Get this easy-to-use, open source server.
- JiBXSoap: Take a look at a SOAP Web services implementation of JiBX from the JiBX home page.
Discuss
- Participate in the discussion forum.
- XML zone discussion forums: Participate in any of several XML-centered forums.




