Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

XFire: The easy and simple way to develop Web services

Expose your POJO methods as Web services

  • Print
  • Feedback

Page 2 of 6

  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:

  • Print
  • Feedback

Resources