Web ServicesAccelerate Your Java Web Service Development with XFire

Accelerate Your Java Web Service Development with XFire

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

SOAP Web Services have been around for a number of years now. For many years, the two main Java-based SOAP frameworks were Apache Axis and Sun’s JAX-WS. A new Java-based SOAP framework by Codehaus called XFire now provides an even simpler way to develop SOAP web services and clients. The first article in this series will create a simple SOAP client using XFire, demonstrating how simple it is to work with this Java SOAP framework. The second article will use XFire to create a web service implementation.

XFire: SOAP Web Services Simplified

Generating SOAP Client Java Classes from a WSDL Using XFire

XFire provides an Ant task that allows a user to generate the Java classes for the SOAP client of a web service. It does this by inspecting a WSDL definition for that web service. You are going to point XFire to the XMethods Stock Quote WSDL URL and have it generate your SOAP client Java classes for you. To do this, you need to set up your ‘wsgen’ Ant task in our build.xml file. Add the following to the ‘build.xml’ file:

<?xml version="1.0" encoding="UTF-8"?>
<project default="compile" name="xfireclient">

   <property environment="env"/>

   <property name="wsdlurl"
      value="http://www.webservicex.net/stockquote.asmx?WSDL" />

   <path id="compile.classpath">
      <fileset dir="${basedir}/lib" includes="**/*.jar" />

   <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"

   <target name="wsgen">
      <wsgen outputDirectory="${basedir}/src"
             overwrite="true" />

   <!-- Clean class files -->
   <target name="clean">
         <fileset dir="bin" includes="**/*.class"/>

   <!-- Compiles source files -->
   <target name="compile" depends="clean, wsgen">
      <javac srcdir="src" destdir="bin" debug="on" verbose="off">
            <fileset dir="lib" />

You define a ‘wsdlurl’ property that contains your StockQuote web service WSDL URL. You also specify your Java compilation classpath by using a path variable ‘compile.classpath’. You define the XFire built-in ‘wsgen’ Ant task definition. You then specify your ‘wsgen’ ant target that uses the XFire ‘wsgen’ taskdef, pointing to the ‘wsdlurl’, to generate your StockQuote web service SOAP client Java classes in the ‘net.xmethods.services.stockquote’ package. When you run this task, you will see the following new Java classes in your project:

Figure 2:StockQuote SOAP web service client Java classes generated by XFire

Coding the StockQuote Web Service Client

You create a Java class file called XFireClientTest in the ‘src’ directory. The ‘src’ directory should now look like this:

Figure 3:XFireClientTest Java class

Fill in the XFireClientTest.java file with the following Java code:

import net.xmethods.services.stockquote.StockQuoteClient;
import net.xmethods.services.stockquote.StockQuoteSoap;

public class XFireClientTest
   public static void main(String[] args) {
      StockQuoteClient client = new StockQuoteClient();
      StockQuoteSoap service = client.getStockQuoteSoap();
      String quote = service.getQuote("HOT");

Your client gets the client proxy for the StockQuote service. It then uses it to get the StockQuote service and call the ‘getQuote()’ service method, passing in the company symbol of ‘HOT’. It then prints out the result to the console. That is all there is for you to code; XFire has generated all the other marshalling code that will convert your input parameter into a SOAP request.

Compiling the XFireDemo Project

Now that you have your Stock Quote web service client, you can go ahead and compile your project using Ant. Running ‘ant build.xml’ produces the following output, indicating that it compiled your source code and created your XFireClientTest and supporting class files in the ‘bin’ directory:

Buildfile: C:Documents and SettingsdominicworkspaceXFireDemo
   [delete] Deleting 8 files from C:Documents and Settingsdominic
   [wsgen] Jun 30, 2007 5:02:46 PM
           org.codehaus.xfire.gen.Wsdl11Generator generate
   [wsgen] INFO: Generating code for WSDL at
           http://www.webservicex.net/stockquote.asmx?WSDL with a
           base URI of http://www.webservicex.net/stockquote.asmx?WSDL
   [wsgen] Jun 30, 2007 5:02:51 PM
   [wsgen] INFO: Creating class
   [wsgen] Jun 30, 2007 5:02:51 PM
   [wsgen] INFO: Creating class
   [wsgen] netwebservicexGetQuote.java
   [wsgen] netwebservicexGetQuoteResponse.java
   [wsgen] netwebservicexObjectFactory.java
   [wsgen] netwebservicexpackage-info.java
   [wsgen] netxmethodsservicesstockquoteStockQuoteClient.java
   [wsgen] netxmethodsservicesstockquoteStockQuoteImpl.java
   [wsgen] netxmethodsservicesstockquoteStockQuoteSoap.java
   [javac] Compiling 8 source files to C:Documents and Settings
   [javac] Note: C:Documents and Settingsdominicworkspace
           StockQuoteClient.java uses unchecked or unsafe operations.
   [javac] Note: Recompile with -Xlint:unchecked for details.
Total time: 14 seconds

Running the StockQuote Service SOAP Client Test

You can run your client directly from Eclipse by right-clicking the XFireClientTest class and selecting ‘Run As->Java Application’:

Figure 4:Running XFireClientTest in Eclipse

When you run the SOAP client test class, XFireClientTest, using a company symbol of ‘HOT’ (Starwood Hotels and Resorts), you will get the following response from the Stock Quote web service, indicating a successful test:

      <AnnRange>49.68 - 74.35</AnnRange>
      <Name>STARWOOD HOTELS&R</Name>


This article took a look at XFire, a Java SOAP framework that makes working with web services fast and simple. You created a SOAP client and saw how easy it is to use XFire. In the next article in this XFire series, you will create your own SOAP web service using XFire.


About the Author

Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java- and Ruby-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories