Using Axis2 and JIBX to convert Java classes to Web services, part 2nd

Source: Internet
Author: User
Tags web services wrapper wsdl

Using Axis2 and JIBX to convert Java classes to Web services, part 2nd: Transforming XML into a comprehensive web service

This article supporting source code

Brief introduction

The Apache Axis2 offers several XML solutions that allow you to easily develop powerful WEB services. Axis2 is a very good choice for implementing WEB services. Because Apache Axis2 and Apache Geronimo duo are free and open source, you can consider using both.

In the previous section of this series, I introduced you to the Java classes that were used in two articles in this series. We expose these classes as Web services through WSDL, and then we create a JiBX definition description that will test the WEB service with the JIBX data binding in the text. During the testing process, we created the wrapper class by compiling the JiBX binding class, making the data-binding Class A functional part of the entire WEB service.

Before continuing further development, make sure that all of the JAR files in <axis2_home>/lib, including the JiBX files that you copied in the first part of this series, are under your classpath, and will be there throughout the entire process of this article. Also, make sure that you have Ant installed on your system. Ant is available for download from Apache.

Let's start building a service.

Build Service

First, you need to use the AXIS2 build service so that you can test it using the client you created later. Before the beginning of the previous article, you should have downloaded the necessary tools and set the AXIS2_HOME environment variables. To start creating the service, type the command in Listing 1.

Listing 1. Create a 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

This will create the most basic service for you! The following sections show the notes for each command switch:

The-uri switch tells Axis2 where to find the WSDL file used to generate the service.

The-p switch tells the AXIS2 service to use the Java package class.

The-d jibx option specifies the JIBX data binding.

The-s switch specifies that only synchronized (block) methods are 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 is used to create a Java interface for the service implementation (Simpleserviceskeleton.java).

The-ebinding switch tells Axis2 where to find the JiBX definition documentation.

There is now a Web service, but there is no definition. The next section defines the service.

Define service Backbone

A method in the service skeleton is invoked whenever a successful operation of the WEB service is successfully invoked. The implementation code for the WEB service operation is contained in a file called Simpleserviceskeleton.java (in this case). The file is located in the Src/com/ibm/devworks/xml/simpleservice directory and is an empty file. Define it, as shown in Listing 2.

Listing 2. Define service Backbone

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 contains two methods that correspond to the two actions discussed in the first article in this series. The first method, OneWay, is a one-way (or input only) operation that uses Onewayrequest Java objects as input parameters and does not return a value. The implementation is simply to display the content sent from the client on the screen.

The second method, TwoWay, uses the Twowayrequest Java object as input and returns the Twowayresponse response. This method first displays the Echostring and Booolean fields of the incoming request object, sets the echostring in the response to the Echostring field value in the request, and sets the Invertedboolean value in the Response object to the booo in the Request object The opposite value of the lean value (true set to False, false to true). The response object is then returned to the client.

The service has been defined well. Next, we will build the service and create the JiBX data-binding wrapper class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.