Simple Web Service with XFire

This post describes how to implement as simple http web service using xfire (a next-generation java SOAP framework).

WEB-INF/Web.xml

Configure the XFire servlet in the web.xml:

<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>

WEB-INF/classes/META-INF/xfire/services.xml

Configure the services.xml file for xfire with the details of the service to expose:

<service>

<name>StockQuote</name>
<namespace>http://localhost:8080/xfire-stockquote/services/stockQuote</namespace>
<serviceClass>demo.xfire.service.StockQuoteService</serviceClass>
<implementationClass>demo.xfire.service.StockQuoteService</implementationClass>

</service>

StockQuoteService.java

The service class:

package demo.xfire.service;

import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StockQuoteService {

protected final Log log = LogFactory.getLog(getClass());

public Float getStockQuote(String security)
  {
    Random rand = new Random();
    log.debug("Retrieving quote for "+ security);
    return new Float(rand.nextInt(101));
  }
}

Testing

After the demo application has been deployed the url will be similar to http://localhost:8080/xfire-stockquote/services/StockQuote?wsdl for tomcat.

To run requests against the web service you could use soapUI

The Code

You can download the war file with the code here: zip filexfire-stockquote.war.

If you are not using Tomcat you need to change the location of the log file in the log4j.properties file.

One Response to “Simple Web Service with XFire”

  1. Su123 Says:

    Hi,
    I want to use the property of Jaxb binding of Xfire in my Mule project. Where do i place the services.xml file in the Mule project. I would like to know if there are any good examples regarding the same.

    Thanks in advance

Leave a Reply