Web Service with Spring and XFire

In a previous post I looked at how a simple quote web service with XFire. This post will implement the same webservice using XFire and Spring.

WEB-INF/web.xml

Configure the xfire dispatcher servlet and the spring config files in the web.xml

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext.xml
	classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
	<servlet-name>xfire</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>xfire</servlet-name>
	<url-pattern>/*</url-pattern>
</servlet-mapping>

WEB-INF/applicationContext.xml

Configure the service class in the applicationContext.xml file:

<beans>
    <bean id="stockQuoteService" class="demo.xfire.service.StockQuoteServiceImpl"/>
</beans>

WEB-INF/xfire-servlet.xml

Configure a SimpleUrlHandlerMapping and an XFireExporter to expose the methods of our service class:

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="urlMap">
		<map>
			<entry key="/StockQuoteService">
				<ref bean="stockQuote"/>
			</entry>
		</map>
	</property>
</bean>

<bean id="stockQuote" class="org.codehaus.xfire.spring.remoting.XFireExporter">
	<property name="serviceFactory">
		<ref bean="xfire.serviceFactory"/>
	</property>
	<property name="xfire">
		<ref bean="xfire"/>
	</property>
	<property name="serviceBean">
		<ref bean="stockQuoteService"/>
	</property>
	<property name="serviceClass">
		<value>demo.xfire.service.StockQuoteService</value>
	</property>
</bean>

Service interface

public interface StockQuoteService {
  public Float getStockQuote(String security);
}

Service Implementation

public class StockQuoteServiceImpl {

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-spring-demo/StockQuoteService?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 xfire-spring-stockquote.war

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

4 Responses to “Web Service with Spring and XFire”

  1. Ravi Says:

    Hi,

    Please let me know which version of Tomcat should be used to deploy this war file at below URL , also any configuration settings to be done.

    http://rscreeney.wordpress.com/2006/09/12/web-service-with-spring-and-xfire/

    I tried to deploy on Tomcat 6.0.13, but could not succeed.

    Thanks & Regards,
    Srinivas.

  2. richybalenz Says:

    Hi, i have deployed this war inside TOMCAT/webapps and initialized tomcat TOMCAT/bin/startup and it run.

    Then access to your browser and set the url

  3. Spring y XFire - XperimentoS Says:

    [...] Para más detalles de la implantacion y configuración de Spring hay enlaces como este. [...]

  4. Ken Says:

    I could not download the war file. Can somebody help me. If you have any alternate location I can download the xfire-spring-stockquote.war please let me know.

    thank you

Leave a Reply