Service XFire WebServices on Separate URLs

Service all XFire Web Service request together or Service Requests on separate URLs

web.xml

  • XFireSpringServlet: this servlet will get initialized with every ServiceBean that gets created in the applicationContext.xml. It will then service all the services at a single url /webservices/*
<servlet>
	<servlet-name>XFireServlet</servlet-name>
	<servlet-class>
	org.codehaus.xfire.spring.XFireSpringServlet
	</servlet-class>
</servlet>

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

applicationcopntext.xml

  • ServiceBean: create service beans that the XFireSpringServlet will service.
<!--  XFire -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

<bean name="com.mydomain.MusicAdapter"
class="org.codehaus.xfire.spring.ServiceBean">
	<property name="serviceBean">
		<bean class="com.mydomain.impl.MusicAdapterImpl">
			<constructor-arg>
				<ref bean="com.mydomain.AuthenticationService" />
			</constructor-arg>
			<constructor-arg>
				<ref bean="com.mydomain.service.MusicService" />
			</constructor-arg>
			<constructor-arg>
				<ref bean="com.mydomain.service.StoreService" />
			</constructor-arg>
			<constructor-arg>
				<ref bean="com.mydomain.service.AdministrationService" />
			</constructor-arg>
			<constructor-arg>
				<ref bean="com.mydomain.config.ConfigurationProvider" />
			</constructor-arg>
		</bean>
	</property>
	<property name="serviceFactory">
		<bean class="org.codehaus.xfire.annotations.AnnotationServiceFactory">
		</bean>
	</property>
	<property name="serviceClass" value="com.mydomain.MusicAdapter"/>
</bean>

<bean name="com.mydomain.BookAdapter"
class="org.codehaus.xfire.spring.ServiceBean">
	<property name="serviceBean">
			<bean class="com.mydomain.impl.BookAdapterImpl">
				<constructor-arg>
					<ref bean="com.mydomain.AuthenticationService" />
				</constructor-arg>
				<constructor-arg>
					<ref bean="com.mydomain.service.BookService" />
				</constructor-arg>
				<constructor-arg>
					<ref bean="com.mydomain.service.StoreService" />
				</constructor-arg>
				<constructor-arg>
					<ref bean="com.mydomain.service.AdministrationService" />
				</constructor-arg>
				<constructor-arg>
					<ref bean="com.mydomain.config.ConfigurationProvider" />
				</constructor-arg>
			</bean>
		</property>
		<property name="serviceFactory">
			<bean class="org.codehaus.xfire.annotations.AnnotationServiceFactory">
			</bean>
		</property>
	<property name="serviceClass" value="com.mydomain.BookAdapter"/>
</bean>

Out Come:

http://localhost:8080/myapplication/webservices/* (this will list all services maintained by the xfire servlet)
http://localhost:8080/myapplication/webservices/MusicAdapter?wsdl
http://localhost:8080/myapplication/webservices/BookAdapter?wsdl

Servicing XFire Web Service request on separate URLs:

web.xml

  • The xfire servlet (DispatcherServlet) is configured in its own file.
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
	/WEB-INF/applicationContext.xml
	/WEB-INF/xfire-servlet.xml
	</param-value>
</context-param>

<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>/webservices/*</url-pattern>
</servlet-mapping>

xfire-servlet.xml

  • SimpleUrlHandlerMapping:used to control which xfire based web services are handled at different url locations
  • XFireExporter:used to create and initialize the xfire wrapper for each service that you are exposing
  • AnnotationServiceFactory:used in the XFireExporter initalization process to read your services that are going to be exposed.
  • MusicAdapterImpl and BookAdapterImpl: are the services that we are exposing
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >

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

<bean name="MusicAdapterImpl" class="com.mydomain.impl.MusicAdapterImpl">
	<constructor-arg>
		<ref bean="com.mydomain.AuthenticationService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.MusicService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.StoreService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.AdministrationService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.config.ConfigurationProvider" />
	</constructor-arg>
</bean>

<bean name="BookAdapterImpl" class="com.mydomain.impl.BookAdapterImpl">
	<constructor-arg>
		<ref bean="com.mydomain.AuthenticationService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.BookService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.StoreService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.service.AdministrationService" />
	</constructor-arg>
	<constructor-arg>
		<ref bean="com.mydomain.config.ConfigurationProvider" />
	</constructor-arg>
</bean>

<bean name="AnnotationServiceFactory"  class="org.codehaus.xfire.annotations.AnnotationServiceFactory" />

<bean name="MusicAdapter_1_0" class="org.codehaus.xfire.spring.remoting.XFireExporter">
	<property name="serviceFactory" ref="AnnotationServiceFactory" />
	<property name="xfire" ref="xfire"/>
	<property name="serviceBean" ref="MusicAdapterImpl" />
	<property name="serviceClass" value="com.mydomain.MusicAdapter" />
</bean>

<bean name="BookAdapter" class="org.codehaus.xfire.spring.remoting.XFireExporter">
	<property name="serviceFactory" ref="AnnotationServiceFactory" />
	<property name="xfire" ref="xfire"/>
	<property name="serviceBean" ref="HialServiceImpl" />
	<property name="serviceClass" value="com.mydomain.BookAdapter" />
</bean>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="urlMap">
		<map>
			<entry key="/1.0/MusicAdapter">
				<ref bean="MusicAdapter_1_0"/>
			</entry>
			<entry key="/BookAdapter">
				<ref bean="BookAdapter"/>
			</entry>
		</map>
	</property>
</bean>

</beans>

Out Come:

http://localhost:8080/myapplication/webservices/1.0/MusicAdapter?wsdl
http://localhost:8080/myapplication/webservices/BookAdapter?wsdl
It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply