Start Writing Web Services Today (cont.)

A Simple Web Service
To demonstrate a simple Web service, we will create a Java client to access an existing Web service available on the Net. The service we will be accessing calculates the rate of exchange between the currencies of two countries. A SOAP message is sent to the service containing two parameters of type String representing the names of the two countries. The service then parses the SOAP envelope, extracts the parameter values, processes the request and sends a response back to the client as type double. The details for this service can be found at http://www.xmethods.com/ve2/ViewListing.po?serviceid=5.

Editor's Note: This article (and its associated source code) has been modified from its original version. The original story used code that would run on the Alpha 3 version of Apache's Axis. With a Beta 1 version of Axis now available, we have updated the article and code to run on this later version. Please make sure that you are using the newer, Beta 1 version of Axis before you begin any project using this tutorial.

To build this Web service client, we will use Apache Axis, which can be downloaded from Apache's Web site site (This article assumes the use of the beta1 version of Axis). Axis is a complete rearchitecture of the Apache SOAP 2.x project from the ground up. The result is a faster, more flexible, more powerful, and easier-to-use framework for designing, developing, and deploying SOAP-based Web services. To use the Apache Axis client API, you will also need a namespace-aware XML parser such as Apache Xerces.

Once you have obtained a copy of the Apache Axis distribution and a JAXP-compliant parser, then you need to make their classes available for your applications. To do this, place three JAR files into the computer's classpath environment variable: %AXIS_DIST%/lib/axis.jar, %AXIS_DIST%/lib/jaxrpc.jar, %AXIS_DIST%/lib/commons-logging.jar, %AXIS_DIST%/lib/tt-bytecode.jar and xerces.jar, or crimson.jar and jaxp.jar (or whatever parser you are using).

Now you are ready to construct the client application. We begin by importing the necessary classes into our application:

  import org.apache.axis.client.Service;
  import org.apache.axis.client.Call;
  import javax.xml.rpc.namespace.QName;
The next step will be to declare a few constants to represent the fixed properties of the Web service that we will be accessing. With SOAP-RPC (two-way synchronous messaging), this means that we need to identify a URL where the service endpoint resides, a unique namespace URI for the method, and the method name of the service itself. So, we declare three variables to contain the properties required for locating and accessing our Web service:
  	private static final String  
	ENDPOINT_URL = "http://services.xmethods.net:80/soap";
    private static final String  
	OPERATION_NAME = "getRate";
    private static final String  
	NAMESPACE_URI = "urn:xmethods-CurrencyExchange";
The Apache Axis client API encapsulates the construction of a SOAP envelope, marshalling of parameters, as well as the service invocation and unmarshalling of return value, all within a Call object.
    Service   service;
    Call      call;
    Object[]  params;
    Float     response;

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

      call.setTargetEndpointAddress( new java.net.URL(ENDPOINT_URL) );
      call.setOperationName( new QName( NAMESPACE_URI, OPERATION_NAME ) );
Once we have prepared the Call object, we need to retrieve the names of the two countries that were passed in at the command line and place them within an Object array. A full list of the available countries can be found at the bottom of the service description page indicated earlier.
      params = new Object[] { args[0], args[1] };
Finally, we are ready to invoke the service, passing in the Object[] and casting the result to type Float.
response = ( Float )call.invoke( params );

      System.out.println( response.floatValue() );

    } catch( Exception e ) {
       e.printStackTrace();
    }//end try/catch
At this point, you should be ready to compile the code and test it by running the application and passing in the names of two countries from the aforementioned list into the application.


Web Service Examples A Complex Web Service


In this Article
Introduction A Complex Web Service
Web Service Examples Deploying the Service
A Simple Web Service Download the Code
 





FEATURE SOFTWARE:
dtSearch Web
Add power searching to your web site.
Buy Now!

Encrypt It
Encrypt and Decrypt Data, Passwords and Files within your application.
Buy Now!

Standards Organizations and Web Service Coalitions

Key Platforms

Pure-Play Web Services Vendors

Web Service Technologies or Components

From Sun.com

For Further Reading

Discussion Groups
Java Web Services
.NET Web Services

Back to the Special Report

Java Zone

2001 Special Report: Judging Java

TALK BACK
How do you intend to make your entry into Web services? What kinds of applications do you think you'll select first for web services deployment? Tell us in the Java Web Services newsgroup.


Sponsored Links

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map
Jupiterweb networks

internet.comearthweb.comDevx.comClickZ

Search Jupiterweb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents, and JupiterImages

Copyright 2004 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | E-mail Offers

Copyright Information/Privacy Statement