xfire1.26 spring2.5 整合
?开发步骤:
? ? 一, 环境搭建
? ? ? ?新建Web工程(或者直接建service项目),名为webservice_helloworld
? ? ? ?加入jar包 :
? ? ? ? ? ? ?activation-1.1.jar、commons-beanutils-1.7.0.jar、commons-codec-1.3.jar、commons-httpclient.jar、commons-logging-1.0.4.jar、jaxen-1.1-beta-9.jar、jaxws-api-2.0.jar、jdom-1.0.jar、jsr173_api-1.0.jar、mail-1.4.jar、saaj-api-1.3.jar、saaj-impl-1.3.jar、spring-1.2.6.jar、stax-api-1.0.1.jar、wsdl4j-1.5.2.jar、wstx-asl-3.0.1.jar、xbean-2.1.0.jar、xbean-spring-2.5.jar、xfire-aegis-1.2.2.jar、xfire-annotations-1.2.2.jar、xfire-core-1.2.2.jar、xfire-java5-1.2.2.jar、xfire-jaxws-1.2.2.jar、xfire-jsr181-api-1.0-M1.jar、xfire-spring-1.2.2.jar、XmlSchema-1.1.jar
?
? 注:可依次加入也可通过myeclipse导入
?
?
? 二, 代码编写
? ? ??1)web.xml的配置
?
?
<?xml version="1.0" encoding="UTF-8"?><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>
?
? ? ?注:其中需要加载2个配置文件: 一个spring 一个xfire-servlet?
? ? ? ? ? ? ? ? ?2个listener:一个spring启动 。一个 xfire 启动
? ? ? ? ? ? ? ? ?2个servlet :一个创建webservice时就有,一个是配合spring工作。
?
? ?2)Web Service的接口类HelloWorld.java和对应实现类HelloWorldImpl.java
?
?
package webservice;/** *HelloWorld接口 */publicinterface HelloWorld { String sayHelloWorld(String name);}package webservice;/** *HelloWorld的实现类. */publicclass HelloWorldImpl implements HelloWorld { public String sayHelloWorld(String name) { String helloWorld = "hello," + name; return helloWorld; }}?
?
??3)Spring配置文件applicationContext.xml和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> <bean id="HelloWorldBean" name="code">Line 5 in XML document from ServletContext resource [/WEB-INF/xfire-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
? ? ? ? ? ? ? 下面是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 /> </entry> </map> </property> </bean> <!-- 使用XFire导出器 --> <bean id="baseWebService" 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环境,即serviceFactory和xfire,这是标准的配置。ServiceFactory是XFire的核心类,它可以将一个POJO生成为一个Web Service。
在本实例中,我们通过定义一个baseWebService,其余的webService配置都将该bean作为父bean,这样可以简化Spring的配置,不需要多次引入serviceFactory和xfire ( parent="baseWebService")
3. Web Service的测试
在浏览器中输入地址:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl?会看到wsdl文件
如果输入为:http://localhost:8080/webservice_helloworld/HelloWorldService.ws?可能会看到 ? ? ??Invalid SOAP request. 后面加上 ??wsdl ??即可
?
?
一: 若在同一个项目中测试很简单
首先访问成功 。测试类如下
??package com.client;
import java.net.MalformedURLException;import java.util.List;import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;import com.service.IService;import com.vo.User;public class TestClient {/** * 测试Xfire的客户端 */public static void main(String[] args) {// 创建一个服务对象(远程调用),使用服务的工厂创建ws的服务对象Service srcvMode = new ObjectServiceFactory().create(HelloWorld.class);//创建调用代理工厂,获取服务XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());String serviceURL="http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl";HelloWorld HW=null;//获取服务try {HW=(HelloWorld ) factory.create(srcvMode, serviceURL);} catch (MalformedURLException e) {e.printStackTrace();}//调用简单的WS方法String strHW.sayHelloWorld("你是大笨象"); System.out.println();}}? ? ? ? ? ? ? ? ? 也可以这样测试
?
Client client = new Client(new URL("http://localhost:8080/webservice_helloworld/HelloWorldService.ws?wsdl"))//invoke("方法名", 参数值);Object[] obj=client.invoke("sayHelloWorld", objArray); System.out.println(results[0]);? ? ? ? ? 也可以通过myEclipse工具反向生成service文件
? ? ? ? ?具体步骤为:?
? ? ? ? ?1, 新建一个项目,导入xfire所需jar包(选择项目>右键>NyEclipse>Add XFire Service Cp..>jar包选Core与HTTP其它按需>)
? ? ? ? ? 2,new一个Web Service Client (new>other>Web Service Client>next>选择项目和jar包:手动加入选第一个>next>填写WSDL地址与生成文件所在位置>next>此时会检查配置信息且会访问WSDL地址:需保证路径可访问,若没有错误提示就可以next了)
? ? ? ? ? 3,查看生成文件,其中有一个文件叫xxxServiceClient.java(对应着接口名称的客户端)?
? ? ? ? ? ? ? ? 打开看main 方法 这是一个调用实例 ?
?
XxxrServicePortType service = client.getxxxServiceHttpPort();
? ? ? ? ? ? 使用service即可调用方法;
? ? ? ? ? ? ??service.sayHelloWorld("你真的是一个小笨象")
?
?
?
?
?
?
?