Tried setting up XFire with Spring and thought I’d share that experience. One more place to come for this information will not hurt ah!
Once again I used Maven to build my test application. At the bottom of this article you will find a download link for the entire application.
I have used Axis in the past and wanted to try out some other frameworks. At the same time I absolutely needed the framework to support JSR 181 (web service annotations), required the framework to integrate with Spring and relatively simple configuration. Oh also I did not want to write any WSDL. This example is an RPC based web service (unlike my previous article on document based web service with Spring-WS). I will after this article also start using Axis2, since I have been an Axis fan for many years.
JSR 181 is important to me. I think annotations are the right way to go for most simple tasks that do not require a lot of input. The web service annotations are good example about where annotations are the right fit. I have seen examples of annotations where it would be far easier and clearer to put it into the XML style configuration. Some folks are anti-annotations and I think that attitude is not the best. Use it where it makes the most sense.
Lets view the echo service java POJO code.
1 2 3 4 5 |
1: package com.aver; 2: public interface EchoService { 3: public String printback(java.lang.String name); 4: } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
1: package com.aver; 2: import java.text.SimpleDateFormat; 3: import java.util.Calendar; 4: import javax.jws.WebMethod; 5: import javax.jws.WebParam; 6: import javax.jws.WebResult; 7: import javax.jws.WebService; 8: @WebService(name = "EchoService", targetNamespace ="http://www.averconsulting.com/services/EchoService") 9: public class EchoServiceImpl implements EchoService { 10: @WebMethod(operationName = "echo",action = "urn:echo") 11: @WebResult(name = "EchoResult") 12: public String printback(@WebParam(name ="text") String text) { 13: if (text== null || text.trim().length() == 0) { 14: return "echo: -please provide a name-"; 15: } 16: SimpleDateFormat dtfmt = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a"); 17: return "echo: '" + text + "' received on " + dtfmt.format(Calendar.getInstance().getTime()); 18: } 19: } |
As you can see above I have made liberal use of JSR 181 web service annotations.
- @WebService declares the class as exposing a web service method(s).
- @WebMethod declares the particular method as being exposed as a web service operartion.
- @WebParam gives easy-to-read parameter names which will show up in the auto-generated WSDL. Always provide these for the sake of your consumers sanity.
- Also you can see that the java method is named ‘printback’ but exposed as name ‘echo’ by the @WebMethod annotation.
Here is the web.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
1: <?xmlversion="1.0" encoding="ISO-8859-1"?> 2: <!DOCTYPE web-app 3: PUBLIC "-//Sun Microsystems, Inc.//DTDWeb Application 2.3//EN" 4: "http://java.sun.com/dtd/web-app_2_3.dtd"> 5: <web-app> 6: <context-param> 7: <param-name>contextConfigLocation</param-name> 8: <param-value>classpath:org/codehaus/xfire/spring/xfire.xml/WEB-INF/xfire-servlet.xml</param-value> 9: </context-param> 10: <listener> 11: <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12: </listener> 13: <servlet> 14: <servlet-name>XFireServlet</servlet-name> 15: <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> 16: </servlet> 17: <servlet-mapping> 18: <servlet-name>XFireServlet</servlet-name> 19: <url-pattern>/servlet/XFireServlet/*</url-pattern> 20: </servlet-mapping> 21: <servlet-mapping> 22: <servlet-name>XFireServlet</servlet-name> 23: <url-pattern>/services/*</url-pattern> 24: </servlet-mapping> 25: </web-app> |
The web.xml configures the ‘XFireSpringServlet‘ and sets up the Spring listener. Straightforward.
Finally here is the xfire-servlet.xml (this is our spring configurationfile).
1 2 3 4 5 6 7 8 9 10 11 |
1: <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/> 2: <bean id="jsr181HandlerMapping"xfire_echo.jar class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> 3: <property name="typeMappingRegistry"> 4: <refbean="xfire.typeMappingRegistry" /> 5: </property> 6: <property name="xfire" ref="xfire" /> 7: <property name="webAnnotations" ref="webAnnotations" /> 8: </bean> 9: <bean id="echo"class="com.aver.EchoServiceImpl" /> 10: </beans> |
- Sets up the xfire bean to recognize jsr 181 annotations.
- Last bean is our echo service implementation bean (withannotations).
That is it. Build and deploy this and you should see the WSDL at http://localhost:9090/echoservice/services/EchoServiceImpl?wsdl.
Click here to download the Maven based project code. To build run:
- mvn package
- mvn jetty:run