技术开发 频道

使用XFire+Spring构建Web Service



3. Web Service的测试
    在上一步操作完成之后,我们的这个简单的Web Service已经编写完毕,下面让我们来看看自己的劳动成果吧。
    在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl,我们可以看到HelloWorldService对应的WSDL信息,阅读这个WSDL文档,我们可以知道HelloWorld的sayHelloWorld方法已经被成功地发布为Web Service了。只要拿到这个WSDL就可以开发相应的客户端调用程序了。
XFire为访问服务端Web Service提供了各种方便的方式:我们一般根据服务地址和窄接口类创建客户调用程序。
  在不能获得服务窄接口类的情况下,XFire允许我们通过WSDL文件生成客户端调用程序,通过指定服务接口的方式调用服务。
1)通过WSDL文件生成客户端调用程序
首先我们通过http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl我们可以获得WSDL文件HelloWorldService.wsdl,并将其放在src目录下面,接着我们通过程序访问该WSDL文件,并调用需测试的方法。此时测试类WebServiceClientTest.java的内容如下所示:

package test; import org.codehaus.xfire.client.Client; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import webservice.HelloWorld; /** * Copyright 2007 GuangZhou Amigo. * All right reserved. * HelloWorld的webservice的测试类. * @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a> * @version 1.0 * Creation date: 2007-9-16 - 下午05:36:05 */ public class WebServiceClientTest { HelloWorld helloWorld = null; public static void main(String[] args) throws Exception { WebServiceClientTest test = new WebServiceClientTest(); test.testClient(); } public void testClient() throws Exception { String wsdl = "HelloWorldService.wsdl"; //对应的WSDL文件 Resource resource = new ClassPathResource(wsdl); Client client = new Client(resource.getInputStream(), null); //根据WSDL创建客户实例 Object[] objArray = new Object[1]; objArray[0] = "阿蜜果"; //调用特定的Web Service方法 Object[] results = client.invoke("sayHelloWorld", objArray); System.out.println("result: " + results[0]); } }
运行该类,可得到如下输出结果:
result: hello,阿蜜果
可看出运行结果正确。
2)根据服务地址创建客户端调用程序
    接着让我们来看一个根据服务地址创建客户端调用程序的例子。我们可以通过测试类来测试Web Service的正确性,下面让我们来看一个简单的测试类,首先我们在src/test目录建立WebServiceClientTest.java文件,并在src目录下建立客户端调用的Spring配置文件client.xml。在client.xml配置文件中我们定义了一个testWebServicebean,该bean访问wsdlDocumentUrlhttp://localhost:8080/webservice_helloworld/HelloWorldService.ws?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>webservice.HelloWorld</value> </property> <property name="wsdlDocumentUrl"> <value>http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl</value> </property> </bean> </beans>
WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示:


package test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import webservice.HelloWorld; /** * HelloWorld的webservice的测试类. */ public class WebServiceClientTest { HelloWorld helloWorld = null; public static void main(String[] args) { WebServiceClientTest test = new WebServiceClientTest(); test.testClient(); } public void testClient() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "client.xml"); helloWorld = (HelloWorld) ctx.getBean("testWebService"); System.out.println(helloWorld.sayHelloWorld("阿蜜果")); } }
在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息:
    hello,阿蜜果
    由此可看出调用Web Service成功。
四.总结
    与Axis相比,在实施Web Service时XFire更加简洁高效,并且XFire对Spring提供了强大的支持,可以非常方便地在Spring中使用XFire实施Web Service,因此XFire在短短的时间里成为了受Web Service开发者喜爱的框架。
  XFire为客户端提供了多种访问Web Service的方式,如果可以获取客户端的窄接口类,则可以采用窄接口类调用Web Service。如果仅能获取WSDL,XFire也可以采用动态反射的机制调用Web Service。XFire为Eclipse提供了一个可以根据WSDL生成客户端存根代码的插件,相信XFire也将为其它非Java语言提供类似的插件。
技术可用性的一个很大的标准是它是否方便测试,XFire提供多种方式进行Web Service的测试,简单方便,给Web Service开发人员的测试工作带来了福音。
在本文中,笔者通过一个简单的helloWorld的Web Service例子,详细地说明了用XFire+Spring构建Web Service时配置文件的相关配置,以及测试的各种方法,也让读者见识了XFire与Spring的无缝集成,希望对读者学习XFire有点帮助。
0
相关文章