前言:
XFire是新一代WebService框架,同时也支持与Spring集成,帮助咱们方便快速地在Spring框架中开发WebService应用。html
本节主要介绍XFire+Spring集成的2种经常使用的方法来实现简单的WebService应用
一、使用XFire的XFireSpringServlet和ServiceBean
二、使用Spring的DispatcherServlet与XFire的XFireExporterjava
准备工做:
XFire官方网站下载地址:http://xfire.codehaus.org/Downloadweb
开发环境:
Window7 + Eclipse3.3 + Tomcat6 + JDK-1.6spring
XFire服务端和客户端工程预览图:tomcat
First-使用XFire的XFireSpringServlet和ServiceBeanapp
1、Server-服务端实现步骤: 1.建立service接口->2.建立Service接口的实现类->3.web.xml配置(XFireSpringServlet)->4.配置ServiceBean->5.服务发布
框架
一、建立service接口
eclipse
package webjar.foo; import common.MyPojo; public interface MyDispatcherServletXFire { String divide(int dividend, int divisor); MyPojo getMyPojo(MyPojo pojo); }
二、建立Service接口的实现类jsp
package webjar.foo; import common.MyPojo; // org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire { @Override public String divide(int dividend, int divisor) { return dividend + " ÷ " + divisor + " = " + (dividend / divisor); } @Override public MyPojo getMyPojo(MyPojo pojo) { System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo); pojo.setName("DispatcherServlet and XFireExporter to publish xfire services"); pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" }); return pojo; } }
三、在web.xml文件中进行XFire拦截配置(XFireSpringServlet)maven
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring加载的配置文件--> <context-param> <param-name>contextConfigLocation</param-name> <!-- 能够再这里加入xfire.xml也能够在applicationContext.xml中引入 --> <param-value> classpath:org/codehaus/xfire/spring/xfire.xml classpath:context-webservice.xml </param-value> </context-param> <!-- Xfire Servlet --> <servlet> <servlet-name>XFireServlet</servlet-name> <display-name>XFire Servlet</display-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
四、新增context-webservice.xml文件,里面进行WebService服务的发布的基本配置(ServiceBean)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- Spring和XFire实现方法一 --> <!-- org.codehaus.xfire.spring.XFireSpringServlet与org.codehaus.xfire.spring.ServiceBean结合实现XFire --> <!-- Service实现类--> <bean id="myXFireSpringServlet" class="xfirejar.foo.MyXFireSpringServletImpl" /> <!-- 这里的name属性并非调用时的Service名字;调用时要用类名,而不能直接使用myXFireService --> <bean name="myXFireService" class="org.codehaus.xfire.spring.ServiceBean"> <!-- Service实现类 --> <property name="serviceBean" ref="myXFireSpringServlet" /> <!-- Service接口 --> <property name="serviceClass" value="xfirejar.foo.MyXFireSpringServlet" /> <property name="inHandlers"> <list> <ref bean="addressingHandler" /> </list> </property> </bean> <bean id="addressingHandler" class="org.codehaus.xfire.addressing.AddressingInHandler" /> </beans>
五、验证服务发布状态
启动tomcat正常,由于本人的web工程名称为XFireFoo,所以在Browser地址输入http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl,
若是显示效果和下面截图信息一致,即说明webservice服务端搭建成功!
2、Client-客户端实现步骤:
A、客户端与WebService服务端在同一应用
一、测试桩示例代码
package xfirejar.foo; import java.net.MalformedURLException; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import common.MyPojo; public class XFireJarFoo { public static void main(String[] args) throws MalformedURLException { // 调用时要用类名(接口名称),而不能直接使用myXFireService String serviceURL = "http://localhost:8080/XFireFoo/remoting/MyXFireSpringServlet"; Service serviceModel = new ObjectServiceFactory().create(MyXFireSpringServlet.class, null); XFireProxyFactory serviceFactory = new XFireProxyFactory(); MyXFireSpringServlet myFire = (MyXFireSpringServlet) serviceFactory.create(serviceModel, serviceURL); System.out.println(myFire.divide(9, 3)); MyPojo pojo = new MyPojo(); pojo.setArray(new String[] { "remoting" }); pojo.setName("XFireJarFoo"); System.out.println(myFire.getMyPojo(pojo)); } }
PS:也可使用XFire中的XFireClientFactoryBean来实现调用,可参见[转]:http://blog.csdn.net/wlbing0625/article/details/7744699
二、测试结果
9 ÷ 3 = 3
MyPojo{name = XFireSpringServlet and ServiceBean to publish xfire services array = [Hi, Welcome to MyXFireSpringServlet !]}
B、客户端与WebService服务端在不一样应用
re: 经过访问http://ip:port/XFireFoo/remoting/MyXFireSpringServlet?wsdl地址咱们能够获取到wsdl文件,
所以直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy !
Second-使用Spring的DispatcherServlet与XFire的XFireExporter
1、Server-服务端实现步骤: 1.建立service接口->2.建立Service接口的实现类->3.web.xml配置(DispatcherServlet)->4.配置XFireExporter->5.服务发布
一、建立service接口
package webjar.foo; import common.MyPojo; public interface MyDispatcherServletXFire { String divide(int dividend, int divisor); MyPojo getMyPojo(MyPojo pojo); }
二、建立Service接口的实现类
package webjar.foo; import common.MyPojo; // org.springframework.web.servlet.DispatcherServlet与org.codehaus.xfire.spring.remoting.XFireExporter结合实现XFire public class MyDispatcherServletXFireImpl implements MyDispatcherServletXFire { @Override public String divide(int dividend, int divisor) { return dividend + " ÷ " + divisor + " = " + (dividend / divisor); } @Override public MyPojo getMyPojo(MyPojo pojo) { System.out.println("Hi, The client's pojo is :" + System.getProperty("line.separator") + pojo); pojo.setName("DispatcherServlet and XFireExporter to publish xfire services"); pojo.setArray(new String[] { "Hi, Welcome to MyDispatcherServletXFire !" }); return pojo; } }
三、在web.xml文件中进行DispatcherServlet拦截配置(DispatcherServlet)
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Spring加载的配置文件,主要经过ContextLoader中的CONFIG_LOCATION_PARAM = "contextConfigLocation" --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 能够再这里加入xfire.xml也能够在applicationContext.xml中引入 --> <param-value> classpath:org/codehaus/xfire/spring/xfire.xml classpath:context-webservice.xml </param-value> </context-param> <!-- Spring framework --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 注意由于servlet-name为myxfire,固xfire配置文件名应该是myxfire-servlet.xml --> <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>/services/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
四、新增xfire-servlet.xml文件,里面进行WebService服务的发布的基本配置(XFireExporter)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- basic configuration --> <!-- 如果web.xml已经配置了org/codehaus/xfire/spring/xfire.xml,这里就不须要配置了 --> <!-- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> --> <!-- Service实现类--> <bean id="myImpl" class="webjar.foo.MyDispatcherServletXFireImpl" /> <!-- XFire发布服务核心处理类的配置 --> <bean name="/IWebJarService" class="org.codehaus.xfire.spring.remoting.XFireExporter"> <property name="serviceFactory" ref="xfire.serviceFactory" /> <!-- Service实现类 --> <property name="serviceBean" ref="myImpl" /> <!-- Service接口 --> <property name="serviceClass" value="webjar.foo.MyDispatcherServletXFire" /> </bean> </beans>
五、验证服务发布状态
启动tomcat正常,由于本人的web工程名称为XFireFoo,所以在Browser地址输入http://ip:port/XFireFoo/services/IWebJarService?wsdl,
若是显示效果和下面截图信息一致,即说明webservice服务端搭建成功!
2、Client-客户端实现步骤:
A、客户端与WebService服务端在同一应用
一、本地客户端测试桩
package webjar.foo; import java.net.MalformedURLException; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; import common.MyPojo; public class WebJarFoo { public static void main(String[] args) throws MalformedURLException { // 客户端访问时使用xfire-servlet.xml中的XFireExporter配置的name属性(IWebJarService)进行调用 String serviceURL = "http://localhost:8080/XFireFoo/services/IWebJarService"; Service serviceModel = new ObjectServiceFactory().create(MyDispatcherServletXFire.class, null); XFireProxyFactory serviceFactory = new XFireProxyFactory(); MyDispatcherServletXFire myFire = (MyDispatcherServletXFire) serviceFactory.create(serviceModel, serviceURL); System.out.println(myFire.divide(9, 3)); MyPojo pojo = new MyPojo(); pojo.setArray(new String[] { "services" }); pojo.setName("WebJarFoo"); System.out.println(myFire.getMyPojo(pojo)); } }
二、测试结果
9 ÷ 3 = 3
MyPojo{name = DispatcherServlet and XFireExporter to publish xfire services array = [Hi, Welcome to MyDispatcherServletXFire !]}
B、客户端与WebService服务端在不一样应用
re: 经过访问http://ip:port/XFireFoo/services/IWebJarService?wsdl地址咱们能够获取到wsdl文件,
所以直接“使用eclipse自带WEB service client指定wsdl文件,从而反向生成java代码方式”进行webservice服务调用。
具体实现步骤可参见本人已发布的AXIS最佳实践章节中“客户端的开发”,So easy !
XFireFoo服务端和客户端示例代码:XFireFoo