Skip to main content

Transform Java classes into Web services using Axis2 and JiBX, Part 2: Turn your XML into a fully functional Web service

Generate, deploy and test your Web service with Axis2 and JiBX

Tyler Anderson graduated with a degree in computer science in 2004 and a master of science degree in computer engineering in December, 2005, both from Brigham Young University.

Summary:  XML is powerful in that it can be used to define just about anything. What's more, it is the basis for an externally readable format for a majority of applications, most notably for the purposes of this series, Axis2 and JiBX. On top of that, as Web services become more and more ubiquitous, turning your legacy Java® projects into full-fledged Web services is increasingly becoming a priority. Unlike in the past when the automatic generation of Web services was limited to a service and a single class, developers now have the option to generate a service or multiple services from the various Java classes in their existing projects. This article, the second part in a series of 2, uses Axis2 and JiBX to go from XML to a fully functional Web service from existing Java classes.

View more content in this series

Date:  22 Mar 2007
Level:  Intermediate
Activity:  2793 views

Introduction

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.


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 -uri switch tells Axis2 where to find the WSDL file that you want to generate a service from.
  • The -p switch tells Axis2 the Java package class to use for the service.
  • The -d jibx option specifies JiBX data bindings.
  • The -s switch specifies that only the synchronous (blocking) methods will be available.
  • The -ss switch tells Axis2 to build the server side code.
  • The -sd switch tells Axis2 to build a service descriptor (services.xml file).
  • The -ssi switch is optional, and simply creates a Java interface for the service implementation (SimpleServiceSkeleton.java).
  • The -Ebinding switch 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.


Defining the service skeleton

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.


Deploying on Geronimo

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.


Generating the client

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.


Writing test code

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.


Testing

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
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
Displaying the output of the client

Everything is working like a charm. You're ready to perform JiBX magic!


Summary

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.



Download

DescriptionNameSizeDownload method
Part 2 sample codex-jibx2-source.zip24KB HTTP

Information about download methods


Resources

Learn

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

About the author

Tyler Anderson graduated with a degree in computer science in 2004 and a master of science degree in computer engineering in December, 2005, both from Brigham Young University.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML, Java technology, SOA and Web services, Open source
ArticleID=202396
ArticleTitle=Transform Java classes into Web services using Axis2 and JiBX, Part 2: Turn your XML into a fully functional Web service
publish-date=03222007
author1-email=tyleranderson5@yahoo.com
author1-email-cc=dwxed@us.ibm.com

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers