XFire, Spring and JSR181 tutorial

Spring is a marvellous framework but lacks a decent WS support. Thru bizarre components we can achieve Axis integration, but then we come out with another problem: is Axis the better solution? I prefer XFire ‘cause is more configurable and also, it’s faster. The documentancion is short, but I pretend to show you how I’ve configured my webapp using some advanced features like JSR181. You can find more documentation here (xfire) and here (spring).

1. Dependencies
We will start from an existing project where we have spring installed (spring.jar). We would need to add some JAR’s for XFire to run; here they are:

  • jaxen-1.1-beta-8.jar
  • jdom-1.0.jar
  • stax-api-1.0.jar
  • wsdl4j-1.5.1.jar
  • wstx-asl-2.0.3.jar
  • xfire-all-1.0-M6a.jar
  • xfire-jsr181-api-1.0-M1.jar

2. Service Beans
For our service we will need an interface and implementation class, so we will end up with “StoreService.java” (interface) y “StoreServiceImpl.java” (class). I’ve annotated the code so I can have greater control over the service: defining parameter names, namespaces, etc…all in a standarized way.

StoreService.java
@WebService(name=”StoreService”, targetNamespace = “http://qosmeter.nqas.es/Store”)
public interface StoreService {
@WebMethod(operationName=”store”)
public abstract int store(@WebParam(name = “name”)String[] name, @WebParam(name = “value”)String[] value);
}

StoreServiceImpl.java
@WebService(endpointInterface=”es.nqas.qosmeter.server.store.soap.StoreService”)
public class StoreServiceImpl implements StoreService {
public int store(String[] names, String[] values) {

}
}

3. Configuration files
We need to declare a servlet for HTTP access, so we have te following entry in web.xml:
<!– XFire Servlet –>
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

Since this servlet is a Spring servlet, it will route the petitions to spring controllers, which in XFire, are mapped to XFireExporter classes. Also, spring will try to load a “xfire-servlet.xml” file with spring beans declarations, so we will include our decls here:

<beans>

<bean id=”xfire.jsr181WebAnnotations” class=”org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations”/>

<bean id=”xfire.annotationServiceFactory”
class=”org.codehaus.xfire.annotations.AnnotationServiceFactory”>
<constructor-arg index=”0″>
<ref bean=”xfire.jsr181WebAnnotations”/>
</constructor-arg>
<constructor-arg index=”1″>
<ref bean=”xfire.transportManager”/>
</constructor-arg>
<constructor-arg index=”2″>
<ref bean=”xfire.aegisBindingProvider”/>
</constructor-arg>
</bean>

<!– Service Beans –>
<bean name=”/StorePing” class=”org.codehaus.xfire.spring.remoting.XFireExporter”>
<property name=”serviceFactory”>
<ref bean=”xfire.annotationServiceFactory”/>
</property>
<property name=”xfire”>
<ref bean=”xfire”/>
</property>
<property name=”serviceBean”>
<ref bean=”storeBean”/>
</property>
<property name=”serviceInterface”>
<value>es.nqas.qosmeter.server.store.soap.StoreService</value>
</property>
</bean>
</beans>

You can appreciate that I’ve used the “AnnotationServiceFactory”, cause using this factory allows us to use the annotations to control the WSDL output of the service, ie, the Java-SOAP mapping.
Finally, for each service we want to expose we declare a XFireExporter bean, but instead of “id” attribute, we use “name” and the path under the servlet mapping url which corresponds to this endpoint.

4. Additional configuration
In the previous file we are using some beans that have not been declared through Spring, so, where in hell are they? First of all, we have to declare a new listener, more precisely, a Spring Context Listener that will load some configuration files when context is initialized. We must add this element to “web.xml”:

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

Next, we will add a context parameter with the path to the files:

<!– Context Configuration locations for Spring XML files –>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-*.xml
classpath:org/codehaus/xfire/spring/xfire.xml
</param-value>
</context-param>

The listener will instatiate the beans defined inside this files and make them avalaible along the ones in “xfire-servlet.xml”

Anuncios

Un comentario en “XFire, Spring and JSR181 tutorial

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s