Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

XFire: The easy and simple way to develop Web services

Expose your POJO methods as Web services

Web services enable us to build distributed systems where application components on a network can be accessed in a platform-neutral, language-agnostic, and implementation-independent way. It doesn't matter how an application is developed, what language it uses, or what OS platform it runs on. If it is available as a Web service and designed addressing interoperability issues, your application, developed in any language or platform, will be able to utilize its services. That's the main concept of Web services.

To achieve the platform-neutral and implementation-independent accessibility of Web services, the software industry has agreed on a few technologies as standards. Some of them are:

  • XML: The default data format used across all layers of Web services environment.
  • SOAP: The default protocol for packaging and exchanging messages. When first introduced, it was an acronym for Simple Object Access Protocol. But now SOAP is considered a proper noun, a name in itself, as most people now realize it is a misnomer: SOAP is not really for accessing objects. Plus, it is not simple any more.
  • WSDL (Web Services Description Language): The language that describes Web services. Although based on XML and understandable by humans, WSDL is mainly for machine consumption, to be read and understood by client programs.


The following high-level diagram, based on the document "Web Services Architecture" published by the World Wide Web Consortium, shows how all these technologies are engaged in a working environment:

The process showing how core technologies engage in Web services. Click on thumbnail to view full-size image.

Here, Provider is the application component that would provide the service, and Requester is the client program that would consume it. Many other technologies may participate in the interactions, but this figure shows the core components that must be in a Web services environment.

XFire is a free and open source SOAP framework that not only enables you to implement such an environment with great ease and simplicity, but also provides you many advanced features identified in Web services specifications, but not yet available in most commercial or open source tools. You read the words correctly: great ease and simplicity. In this article, you'll see how simple it is to build a Web service with XFire.

If your Web application has a Java class and you want its methods to be exposed as Web services, you may not have to write a single line of additional Java code when you use XFire. Just work on the deployment descriptors and you'll get a Web service. Yes, it's that easy. Let's look at an example.

A simple Java class

Our example is a banking application hosted in Apache Tomcat 5.5.7 and running under J2SE 1.4.2_07. I assume you already know how to write Web applications in Java and deploy onto Apache Tomcat servers. Our Web application is simple; it does just one thing—transfer funds from one account to another. A plain-old Java class BankingService containing a method named transferFunds() does the job for us. It needs four input parameters:

  1. String fromAccount
  2. String toAccount
  3. double amount
  4. String currency


Here is the code:

 package com.mybank.xfire.example;

import java.text.NumberFormat; import java.text.DecimalFormat;

/** XFire WebServices sample implementation class. */ public class BankingService implements IBankingService { //Default constructor. public BankingService(){ } /** Transfers fund from one account to another. */ public String transferFunds( String fromAccount, String toAccount, double amount, String currency){ String statusMessage = ""; //Call business objects and other components to get the job done. //Then create a status message and return. try { NumberFormat formatter = new DecimalFormat("###,###,###,###.00"); statusMessage = "COMPLETED: " + currency + " " + formatter.format(amount)+ " was successfully transferred from A/C# " + fromAccount + " to A/C# " + toAccount; } catch (Exception e){ statusMessage = "BankingService.transferFunds(): EXCEPTION: " + e.toString(); } return statusMessage; } }


Do you see anything exceptional here? Probably not, except the default constructor, which is public. It is required. Otherwise, XFire would not be able to instantiate the class.

Since designing with interfaces is good practice, our Java class also implements an interface named IBankingService. The code is simple:

 package com.mybank.xfire.example;

public interface IBankingService {

public String transferFunds( String fromAccount, String toAccount, double amount, String currency); }


In actual implementation, such a method may include all kinds of complex calls, queries, and processing operations. But our example code is bare minimum so that we can focus on our main objective: exposing the method as a Web service.

You can see that BankingService is a plain Java class, with no code whatsoever to tell whether it should be used in Web services. And that's fine. We don't need to add anything here. All our work will be done in the deployment descriptors.

Deployment descriptors of the Web application

In Java, Web applications are usually configured using at least one deployment descriptor, named web.xml. XFire itself is a servlet-based application. Hence, we need to add necessary references to this file. Then we have to configure the Web service we are creating. We will use a new file named services.xml to do that.

web.xml

First, let's work on web.xml. We need to add the following XFire servlet-related entries:

 <servlet>
        <servlet-name>XFireServlet</servlet-name>
        <display-name>XFire Servlet</display-name>
        <servlet-class>org.codehaus.xfire.transport.http.XfireConfigurableServlet
         </servlet-class>
    </servlet>

<servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/servlet/XFireServlet/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>


services.xml

Now we have to say what our Web services consist of. This is done in a file named services.xml, which is placed under the META-INF/xfire directory. This whole directory is placed under the WEB-INF/classes folder, which is in the standard classpath of Web applications. Here are the basic configuration entries in services.xml:

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Whou can I use(import) a WSDL with XFire?
By fbeli
0 04/18/08 01:37 PM
by fbeli
. How can we capture the soap request in xfire
By Nandan
1 02/24/08 08:38 AM
by silverowl
. Error on trying code in XFire Tutorial
By anandhsridhar
1 02/11/08 02:53 AM
by anandhsridhar
. Where's the WSDL?
By Mike_Miller
1 09/17/07 08:38 AM
by Ruben_Cuba
. Check out the myEclipse tutorial
By DavidVTHokie
0 04/17/07 08:59 AM
by DavidVTHokie
. I am getting WSDLException
By Nandan
1 03/27/07 04:46 AM
by Nandan
. Error in web.xml example
By chadsapp
0 12/07/06 11:58 AM
by chadsapp
. Download is incomplete
By Factor3
3 10/06/06 03:28 AM
by Anonymous
. Cannot get it working this example
By Debasish
0 10/05/06 09:35 AM
by Anonymous
. Exposing a POJO in WSO2 Tungsten is much simpler
By Azeez
0 10/05/06 09:34 AM
by Anonymous
. How to expose method that needs file as input
By Anonymous
0 10/05/06 08:29 AM
by Anonymous
. How to get a war file
By sreenu doosari
1 10/05/06 08:28 AM
by Anonymous
. Spring integration
By Anonymous
1 10/05/06 08:27 AM
by Anonymous
. Great Contribution
By Alberto33
2 10/05/06 06:51 AM
by Anonymous
. Another article about XFire
By mikagoeckel
1 10/05/06 06:15 AM
by Anonymous
. If I export my service with a Spring exporter ...
By dex
2 10/05/06 06:15 AM
by Anonymous
. webservice using Java
By Anonymous
2 10/05/06 04:00 AM
by Anonymous
. XFire: The easy and simple way to develop Web se
By JavaWorldAdministrator
1 10/04/06 10:16 AM
by Anonymous
. webservice in java
By R.S.Ramaswamy
1 10/04/06 07:30 AM
by Anonymous
. Typo in web.xml
By Ramesh.narayanan
2 10/04/06 07:29 AM
by Anonymous


Resources