`

使用xfire+spring构建webservice

 
阅读更多

xfire是与Axis2并列的新一代webservice框架,通过提供简单的API支持webservice各项标准协议,帮助你方便快速地开发webservice应用。相对于Axis来说,目前xfire相对受欢迎,加上其提供了和是spring集成的支持,在目前的webservice开源社区拥有众多的追随者。并且因为xfirespring提供的支持,使得我们可以很容易在spring中使用xfire构建webservice应用。

 

xfireAxis2相比具有如下特征:

1.支持一系列webservice的新标准--JSR181WSDL2.0JAXB2WS-Security等;

2.使用Stax解释XML,性能有了质的提高。xfire采用WoodstoxStax实现;

3.容易上手,可以方便快速地从pojo发布服务;

4.支持springPicoPlexusLoom等容器;

5.灵活的Binding机制,包括默认的Aegis,xmlbeans,jaxb2,castor

6.高性能的SOAP 栈设计;

 

xfireAxis1性能的比较如下:

1.xfireAxis1.32-6倍;

2.xfire的响应时间是Axis1.31/21/5

 

xfirewebservice框架中开始较晚,它从现有的框架中借鉴了许多优秀的理念,力争将webservice的应用开发难度降到最低。此外,还提供了各种绑定技术,支持多种传输协议,对webservice体系中许多新的规范提供了支持。

下面让我们来看一个xfirespring集成的helloWorld的简单例子。

一.实现的功能和特点

本例具有如下功能和特点:

1.基于J2EE平台的webservice服务;

2.开发方便,配置简单;

3.与spring无缝集成;

xfire是完全基于流数据处理进行工作的系统,这意味着xfire不是将整个SOAP文档缓存在内存中,而是以管道的方式接收SOAP流数据。这种工作方式的转变带来了可观的性能回报,同时节省了内存的占用。

  xfire从管道中接收一个SOAP请求到返回一个SOAP响应,会经历一系列的阶段。在管道调用的任何一个阶段,xfire都可以添加一些额外的Handler,在对消息进行加工处理后再传入到下一个阶段中。在SOAP请求消息对webservice发起真正调用之前,分别会经过传输、预转发、转发、策略实施、用户信息处理、预调用、服务调用等阶段。

二.开发步骤

1. 工程与环境的建立

新建名为webservice_helloworld工程,在WEB-INF/lib目录下添加以下jar包:

 

为了后续的开发和测试,在src目录下分别建立testwebservice目录,分别用于存放测试文件和webservice的相关类。

2webservice实现的编写

在本例中,我们只是做一个helloWorld的简单例子。webservice服务端提供一个根据输入的名字信息回复相应的helloWorld信息的。例如,当名字为阿蜜果时,恢复信息为hello,阿蜜果。下面让我们一步一步来开始进行编码。

(1)web.xml的配置

一般情况下,我们通过HTTP作为webservice的传输协议,这样我们只需启动一个Web服务器(Tomcat,在本例中使用的是Tomcat5.5.20),这样客户端就可以通过 HTTP访问到webservice服务。为了集成spring容器,xfire专门提供一个XFireSpringServlet,我们可以在web.xml中配置该servlet,将spring容器中定义的webservice在某个URI下发布。为了能正确使用xfire,需在web.xml中进行相应配置,在该文件中配置xfireservletservlet-mapping。同时因为本实例需要将xfire集成到spring中,因而需要在web.xml文件中加载spring的相应配置文件。在本实例中,我们首先在WEB-INF下建立两个配置spring配置文件,一个为applicationContext.xml,该文件用来定义本工程的bean,一个为xfire-servlet.xml,用来配置xfire的相关bean。修改后的web.xml的内容如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>webservice_helloworld</display-name>

	<!-- begin spring配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/xfire-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<!-- end spring配置 -->

	<!-- begin xfire 配置 -->
	<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>/</url-pattern>
	</servlet-mapping>
	<servlet>
		<!-- 配合spring容器中xfire一起工作的servlet-->
		<servlet-name>xfireServlet</servlet-name>
		<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>xfireServlet</servlet-name>
		<!-- 在这个URI下开放webservice服务 -->
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
	<!-- end xfire 配置 -->

</web-app>

 

(2)webservice的接口类HelloWorld.java和对应实现类HelloWorldImpl.java

为了用webservice完成HelloWorld功能,我们首先在src/webservice目录下建立接口类HelloWold.java。它仅包含一个sayHelloWorld(String name)的方法,其详细内容如下:

package com.webservice;

/**
 * HelloWorld的接口类
 */
public interface HelloWorld {

	/**
	 * 对名字为name的人打招呼
	 * @param name 名字
	 * @return 返回打招呼的字符串
	 */
	String sayHelloWorld(String name);
}

 

我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:

package com.webservice;

/**
 * HelloWorld的实现类
 */
public class HelloWorldImpl implements HelloWorld {

	public String sayHelloWorld(String name) {
		return "hello," + name;
	}

}

 

(3)spring配置文件applicationContext.xmlxfire-servlet.xml的配置

首先我们在applicationContext.xml文件中配置对应的bean——helloWorldImpl,该xml文件的内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="helloWorldImpl" class="com.webservice.HelloWorldImpl" />
</beans>

 

xfirespring 提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在spring容器中将一个POJO导出为webserviceHelloWorld是业务服务类,拥有一个sayHelloWorld方法,我们希望将此方法开放为webservice。在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为webservice的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为webservice的业务方法。将一个业务类所有需要开放为webservice的方法通过一个窄接口来描述是值得推荐的作法,这让webservice的接口显得很干净。其次,xfire的导出器也需要服务接口的支持,因为它采用基于接口的动态代理技术。窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据,为了简化实例,我们在此简化了实例。

下面让我们看看在xfire-servlet.xml文件中导出器的设置,该文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<!-- 引入xfire预配置信息 -->
	<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
	<!-- 定义访问的url -->
	<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="urlMap">
			<map>
				<entry key="/helloWorldService">
					<ref bean="helloWorldService" />
				</entry>
			</map>
		</property>
	</bean>

	<!-- 使用xfire导出器 -->
	<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter"
		lazy-init="false" abstract="true">
		<!-- 引用xfire.xml中定义的工厂 -->
		<property name="serviceFactory" ref="xfire.serviceFactory" />
		<!-- 引用xfire.xml中的xfire实例 -->
		<property name="xfire" ref="xfire" />
	</bean>
	<bean id="helloWorldService" parent="baseWebService">
		<!-- 业务服务bean -->
		<property name="serviceBean" ref="helloWorldImpl" />
		<!-- 业务服务bean的窄接口类 -->
		<property name="serviceClass" value="com.webservice.HelloWorld" />
	</bean>
</beans>

 

在上面的配置中,引入了xfire.xml这个spring配置文件。它是在xfire核心jar包中拥有一个预定义的spring配置文件,它定义了xfirespring中必须用到的一些Bean和资源。通过XFireExporter将业务类导出为webservice,对于任何导出器,我们都需要引入xfire环境,即serviceFactoryxfire,这是标准的配置。serviceFactoryxfire的核心类,它可以将一个POJO生成为一个webservice

在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化spring的配置,不需要多次引入serviceFactoryxfire

3. webservice的测试

在浏览器中输入地址:

http://127.0.0.1:9900/webservice_helloworld/helloWorldService?wsdl,我们可以看到helloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorldsayHelloWorld方法已经被成功地发布为webservice了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。

xfire为访问服务端webservice提供了各种方便的方式,我们一般根据服务地址和窄接口类创建客户调用程序。

  在不能获得服务窄接口类的情况下,xfire允许我们通过WSDL文件生成客户端调用程序,通过指定服务接口的方式调用服务。

1)通过WSDL文件生成客户端调用程序

首先我们通过

http://127.0.0.1:9900/webservice_helloworld/helloWorldService?wsdl我们可以获得WSDL文件HelloWorldService.wsdl,并将其放在src目录下面,接着我们通过程序访问该WSDL文件,并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示:

package com.test;

import org.codehaus.xfire.client.Client;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * HelloWorld的webservice的测试类
 */
public class WebServiceClientTest {

	public void testClient() throws Exception {
		// (1)通过WSDL文件生成客户端调用程序
		Resource resource = new ClassPathResource("HelloWorldService.wsdl");
		Client client = new Client(resource.getInputStream(), null);// 根据WSDL创建客户实例
		Object[] objArray = new Object[1];
		objArray[0] = "阿蜜果";
		// 调用特定的webservice方法
		Object[] results = client.invoke("sayHelloWorld", objArray);
		System.out.println(results[0]);
	}

	public static void main(String[] args) throws Exception {
		new WebServiceClientTest().testClient();
	}
}

 

运行该类,可得到如下输出结果:

result: hello,阿蜜果

可看出运行结果正确。

2)根据服务地址创建客户端调用程序

接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试webservice的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立WebServiceClientTest.java文件,并在src目录下建立客户端调用的spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebServicebean,该bean访问wsdlDocumentUrlhttp://127.0.0.1:9900/webservice_helloworld/helloWorldService?wsdlWSDL。该xml文件的详细内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="testWebService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
		<property name="serviceClass">
			<value>com.webservice.HelloWorld</value>
		</property>
		<property name="wsdlDocumentUrl">
			<value>http://127.0.0.1:9900/webservice_helloworld/helloWorldService?wsdl</value>
		</property>
	</bean>
</beans>

 

WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示:

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.webservice.HelloWorld;

/**
 * HelloWorld的webservice的测试类
 */
public class WebServiceClientTest {

	public void testClient() throws Exception {
		// (2)根据服务地址创建客户端调用程序
		ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
		HelloWorld helloWorld = (HelloWorld) ctx.getBean("testWebService");
		System.out.println(helloWorld.sayHelloWorld("阿蜜果"));

	}

	public static void main(String[] args) throws Exception {
		new WebServiceClientTest().testClient();
	}
}

 

在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息:

hello,阿蜜果

由此可看出调用webservice成功。

四.总结

Axis 相比,在实施webservicexfire更加简洁高效,并且xfirespring提供了强大的支持,可以非常方便地在spring中使用xfire实施webservice,因此xfire在短短的时间里成为了受webservice开发者喜爱的框架。

xfire为客户端提供了多种访问webservice的方式,如果可以获取客户端的窄接口类,则可以采用窄接口类调用webservice。如果仅能获取WSDLxfire也可以采用动态反射的机制调用webservicexfireeclipse提供了一个可以根据WSDL生成客户端存根代码的插件。

  • 大小: 5.7 KB
分享到:
评论

相关推荐

    使用XFire+Spring构建WebService

    NULL 博文链接:https://di201yao.iteye.com/blog/310851

    ssh及XFire+Spring构建WebService

    关于ssh demo 演示验证码及图片流显示在界面中,XFire+Spring构建WebService整合

    使用XFire+Spring构建Web+Service

    2 使用Stax解释XML,性能有了质的提高。XFire采用Woodstox 作Stax实现; 3 容易上手,可以方便快速地从pojo发布服务; 4 支持Spring、Pico、Plexus、Loom等容器; 5 灵活的Binding机制,包括默认的Aegis,...

    xfire+spring+maven构建webservice服务器和客户端

    NULL 博文链接:https://jishiweili.iteye.com/blog/2087930

    Web Service框架xfire与spring集成开发流程

    XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,帮助你方便快速地...并且因为XFire为Spring提供的支持,使得我们可以很容易在Spring中使用XFire构建Web Service应用。

    webservice例子

    在Spring中使用XFire构建Web Service应用.例子不错,值得下载看看。

    WebService with Apache CXF

    WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。目前它仍只是 Apache 的一个孵化项目。 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 ...

    CXF WEBSERVICE入门,非常详细实用

    WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ...

    WebSer之CXF框架例子.docx

    Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种...本资源以项目工程实例为例子,讲解如何使用CXF开发WebService服务。

Global site tag (gtag.js) - Google Analytics