使用XFire+Spring构建Web Service
<web-app?xmlns="http://java.sun.com/xml/ns/j2ee"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?version="2.4"?xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee???http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
????<display-name>XFireService</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>*.ws</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下开放Web?Service服务?-->
???????<url-pattern>/service/*</url-pattern>
????</servlet-mapping>
????<!--?end?XFire?配置?-->
</web-app>
?
为了用Web Service完成HelloWorld功能,我们首先在src/webservice目录下建立接口类HelloWold.java。它仅包含一个sayHelloWorld(String name)的方法,其详细内容如下:
3)Spring配置文件applicationContext.xml和xfire-servlet.xml的配置
package?webservice;
/**?*//**
?*HelloWorld的接口类.
?*/
publicinterface?HelloWorld?{
????/**?*//**
?????*对名字为name的人打招呼.
?????*@paramname名字
?????*@return返回打招呼的字符串
?????*/
????String?sayHelloWorld(String?name);
}
我们还需要建立一个对应的实现类,来实现sayHelloWorld的功能,该实现类即为HelloWorldImpl.java。该类的详细内容如下:
package?webservice;
/**?*//**
?*HelloWorld的实现类.
?*/
publicclass?HelloWorldImpl?implements?HelloWorld?{
????public?String?sayHelloWorld(String?name)?{
???????String?helloWorld?=?"hello,"?+?name;
???????return?helloWorld;
????}
}
?
首先我们在applicationContext.xml文件中配置对应的bean——HelloWorldBean,该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="HelloWorldBean"?class="webservice.HelloWorldImpl"/>
</beans> ?
XFire为Spring提供了方便易用的导出器XFireExporter,借助该导出器的支持,我们可以在Spring容器中将一个POJO导出为Web Service。HelloWorld是业务服务类,在此拥有一个sayHelloWorld的方法,我们希望将此方法开放为Web Service。在实际应用中,如果某个类具有众多的方法,而其中的某些方法不需要开放为Web Service的情况下,我们可以定义一个窄接口,该接口中只需定义那些开放为Web Service的业务方法。
将一个业务类所有需要开放为Web Service的方法通过一个窄接口来描述是值得推荐的作法,这让Web Service的接口显得很“干净”。其次,XFire的导出器也需要服务接口的支持,因为它采用基于接口的动态代理技术。
窄接口中的方法在真实的系统中可能需要引用其它的业务类或DAO获取数据库中的真实数据,为了简化实例,我们在此简化了实例。
下面让我们看看在xfire-servlet.xml文件中导出器的设置,该文件内容如下:serviceFactory和xfire,这是标准的配置。ServiceFactory是XFire的核心类,它可以将一个POJO生成为一个Web Service。
<?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.ws">??????????????????
??????????????????<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="HelloWorldBean"?/>
???????<!--?业务服务bean的窄接口类?-->
???????<property?name="serviceClass"?value="webservice.HelloWorld"?/>
????</bean>
</beans>
在上面的配置中,我们可以看到,在该配置文件中引入了xfire.xml这个Spring配置文件。它是在XFire核心JAR包中拥有一个预定义的Spring配置文件,它定义了XFire在Spring中必须用到的一些Bean和资源,需要引入这个预定义的配置文件。从该配置文件中可以看出,我们通过XFireExporter将业务类导出为Web Service,对于任何导出器,我们都需要引入XFire环境,即
在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire。
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;
/**?*//**
?*Copyright2007GuangZhouAmigo.
?*Allrightreserved.???
?*HelloWorld的webservice的测试类.
?*@author<a?href="mailto:xiexingxing1121@126.com">AmigoXie</a>
?*@version1.0
?*Creationdate:2007-9-16-下午05:36:05
?*/
publicclass?WebServiceClientTest?{?
????HelloWorld?helloWorld?=?null;
????publicstaticvoid?main(String[]?args)?throws?Exception?{
???????WebServiceClientTest?test?=?new?WebServiceClientTest();
???????test.testClient();
????}?
????
????publicvoid?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配置文件中我们定义了一个testWebService的bean,该bean访问wsdlDocumentUrl为http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl的WSDL。该xml文件的详细内容如下:???? 在WebServiceClientTest.java文件中获得HelloWorld,并调用它的sayHelloWorld方法来完成测试,该类的详细内容如下所示:??? 在启动webservice_helloworld工程的情况下,运行WebServiceClientTest类,可看到控制台包含如下信息:
<?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>
?
package?test;
import?org.springframework.context.ApplicationContext;
import?org.springframework.context.support.ClassPathXmlApplicationContext;
import?webservice.HelloWorld;
/**?*//**
?*HelloWorld的webservice的测试类.
?*/
publicclass?WebServiceClientTest?{?
????HelloWorld?helloWorld?=?null;
????publicstaticvoid?main(String[]?args)?{
???????WebServiceClientTest?test?=?new?WebServiceClientTest();
???????test.testClient();
????}?
????publicvoid?testClient()?{
???????ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext(
??????????????"client.xml");
???????helloWorld?=?(HelloWorld)?ctx.getBean("testWebService");
???????System.out.println(helloWorld.sayHelloWorld("阿蜜果"));
????}
}
?
??? 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有点帮助。