Sunday, September 16, 2007

xFire (Web Services) + Spring + Struts

Over the last couple of days, I spent few hours doing xFire tutorial. After couple of frustrating hours, things finally started falling in place. For the most part, I just followed the tutorial but I got stuck at few places. Hopefully this post will save you few hours. First, the documentation on xFire site is very good as a starting point.


As you might already know, XfireExporter is implemented as a part of Spring MVC controller which requires the service go thru DispatcherServlet, if you are using with Spring and Struts combination, we can use one of xFire's servlet "org.codehaus.xfire.spring.XFireSpringServlet" (see below)

1. Go to http://xfire.codehaus.org/ and download and configure as indicated (notice the dependencies)

2. Go to web.xml and add following pieces


< context-param> (If you have this as a struts plug in you can delete it from struts-config.xml)
<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>

<!-- XFire WebService support servlet. -->
<servlet>
<servlet-name>XFireServlet </servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>

<!-- Xfire Servlet Mapping -->

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

That’s it, as far as set up is concerned.

3. I usually like to keep my different “applicationContext” files like “applicationContext-dao.xml, applicationContext-soa.xml etc in a separate package called “Config”. If you do the same thing create new xml file called “applicationContext-soa.xml “

4. And in your applcationContext.xml (usually under WEB-INF) add following line to import applicationContext-soa.xml


<import resource="classpath:/com/cma/config/applicationContext-soa.xml" />

4. Open “applicationContext-soa.xml” and paste following code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

<bean id="xfire.xmlbeansServiceFactory"
class="org.codehaus.xfire.xmlbeans.XmlBeansServiceFactory"
singleton="true">
vconstructor-arg index="0">
<ref bean="xfire.transportManager"/>
</constructor-arg>
</bean>

<bean id="echoService" class="com.cma.soa.impl.EchoImpl" />


<bean name="echo"
class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory">
<ref bean="xfire.serviceFactory"/>
<property name="serviceBean">
<ref bean="echoService"/>
<property name="serviceClass">
<value>com.cma.soa.Echo
</property>
</bean>



</beans>
5. Go to http://xfire.codehaus.org/Spring+Remoting to get code for Echo.java and EchoImpl

That’s it, you are in business. Restart your server and go to http://localhost:8080/cma/services/Echo?wsdl and see.

No comments: