XFire实例

1.下载xfire:

下载地址: http://xfire.codehaus.org/Download

xalan

XFire 需要 xalan 项目的支持,然而 1.2.6 版本中并没有带有相应的 jar 文件,因此请访问 xml.apache.org,下载 xalan 项目的二进制包。

 

下载最新的XFIRE发布版本,现在最新的是xfire-distribution-1.2.6.zip

 

 

2.MYECLIPSE中把XFIRE-1.2.6目录LIB所有JAR包添加用户库XFIRE

21ECLIPSE主菜单—WINDOW—PREFERENCE—JAVA—BUILD PATH---USER LIBRARIES

22单击—Add Jars按钮选择XFIRE-1.2.6目录LIB所有JAR包。

3.新建一个WEB工程XFireDemo,在项目XFireDemo右键弹出Properties—JAVA BUILD PATH添加用户库XFire

 

4.添加外部JARxfire-1.2.6/xfire-all-1.2.6.jarJAVA BUILD PATH

5.创建服务接口。

package com.xfire;

public interface ISayHello {

    String sayHello(String name);

}

 

6.创建服务实现类。

package com.xfire;

public class SayHello implements ISayHello {

    public String sayHello(String name) {    

       return "Heollo:"+name;

    }

}

 

7.在SRC目录下新建META-INF文件夹,并在下面新建xfire文件夹,并在该目录下新建services.xml,内容如下:

<beans xmlns="http://xfire.codehaus.org/config/1.0">

  <service>

    <name>SayHelloService</name>

    <namespace>http://xfire.codehaus.org/SayHelloService</namespace>

    <serviceClass>com.xfire.ISayHello</serviceClass>

    <implementationClass>com.xfire.SayHello</implementationClass>  

  </service>

</beans>

 

8.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <servlet>

    <servlet-name>XFireServlet</servlet-name>

    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>

  </servlet>

 

  <servlet-mapping>

    <servlet-name>XFireServlet</servlet-name>

    <url-pattern>/servlet/XFireServlet/*</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>XFireServlet</servlet-name>

    <url-pattern>/services/*</url-pattern>

  </servlet-mapping>

 

</web-app>

 

9.测试WEB服务:

http://localhost:8080/xfire/services/SayHelloService?wsdl

内容如下:---个信息到文档SayHelloService.wsdl

<?xml version="1.0" encoding="UTF-8" ?>

- <wsdl:definitions targetNamespace="http://xfire.codehaus.org/SayHelloService" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://xfire.codehaus.org/SayHelloService" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

- <wsdl:types>

- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://xfire.codehaus.org/SayHelloService">

- <xsd:element name="sayHello">

- <xsd:complexType>

- <xsd:sequence>

  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" />

  </xsd:sequence>

  </xsd:complexType>

  </xsd:element>

- <xsd:element name="sayHelloResponse">

- <xsd:complexType>

- <xsd:sequence>

  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" />

  </xsd:sequence>

  </xsd:complexType>

  </xsd:element>

  </xsd:schema>

  </wsdl:types>

- <wsdl:message name="sayHelloResponse">

  <wsdl:part name="parameters" element="tns:sayHelloResponse" />

  </wsdl:message>

- <wsdl:message name="sayHelloRequest">

  <wsdl:part name="parameters" element="tns:sayHello" />

  </wsdl:message>

- <wsdl:portType name="SayHelloServicePortType">

- <wsdl:operation name="sayHello">

  <wsdl:input name="sayHelloRequest" message="tns:sayHelloRequest" />

  <wsdl:output name="sayHelloResponse" message="tns:sayHelloResponse" />

  </wsdl:operation>

  </wsdl:portType>

- <wsdl:binding name="SayHelloServiceHttpBinding" type="tns:SayHelloServicePortType">

  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />

- <wsdl:operation name="sayHello">

  <wsdlsoap:operation soapAction="" />

- <wsdl:input name="sayHelloRequest">

  <wsdlsoap:body use="literal" />

  </wsdl:input>

- <wsdl:output name="sayHelloResponse">

  <wsdlsoap:body use="literal" />

  </wsdl:output>

  </wsdl:operation>

  </wsdl:binding>

- <wsdl:service name="SayHelloService">

- <wsdl:port name="SayHelloServiceHttpPort" binding="tns:SayHelloServiceHttpBinding">

  <wsdlsoap:address location="http://localhost:8080/xfire/services/SayHelloService" />

  </wsdl:port>

  </wsdl:service>

  </wsdl:definitions>

 

10.客户端程序:--为方便,就在本项目下新建一个SERVLET类测试。

 

package com.control;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.xfire.XFire;

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.xfire.ISayHello;

 

public class SayHelloServlet extends HttpServlet {

    public SayHelloServlet() {

       super();

    }

 

    public void destroy() {

       super.destroy(); // Just puts "destroy" string in log

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

       doPost(request,response);      

    }  

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

        Service serviceModel = new ObjectServiceFactory().create(ISayHello.class);

        XFire xfire=XFireFactory.newInstance().getXFire();

        XFireProxyFactory factory = new XFireProxyFactory(xfire);

        String serviceURL = "http://localhost:8080/xfire/services/SayHelloService";

        ISayHello client=null;

        client=(ISayHello) factory.create(serviceModel,serviceURL);

        System.out.println(client.sayHello("abc"));

    }  

    public void init() throws ServletException {

    }

}

 

11.修改web.xml如下:

  <servlet>

    <servlet-name>SayHelloServlet</servlet-name>

    <servlet-class>com.control.SayHelloServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>SayHelloServlet</servlet-name>

    <url-pattern>/sayHelloServlet</url-pattern>

  </servlet-mapping>

 

12.测试:http://localhost:8080/xfire/sayHelloServlet

 

http://ant.apache.org/bindownload.cgi----ANT下载地址

13.在eclipse中新建一个java工程,并将HelloService.wsdl存到这个工程的文件夹中。编

写一个build.xml文件,采用ant自动生成客户端代码(同时也生成了服务器端的代码骨架,

不过目前来说对我们无用,因为我们不创建服务器端代码)。build.xml文件内容如下:

<?xml version="1.0" encoding="gb2312"?>

<project name="facet" default="help" basedir=".">

<!-- =================================================================== -->

<!-- 设置属性 -->

<!-- =================================================================== -->

<property name="optimize" value="false" />

<property name="debug" value="on" />

<property name="deprecation" value="false" />

<property name="build.lib" value="${basedir}/../lib" />

<property name="sources" value="${basedir}/src" />

<property name="build.classes" value="${basedir}/bin" />

<!-- =================================================================== -->

<!-- 设置类路径 -->

<!-- =================================================================== -->

<path id="classpath">

<pathelement location="${build.classes}" />

<fileset dir="${build.lib}">

<include name="*.jar" />

</fileset>

</path>

<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask"

classpathref="classpath" />

<!-- ================================================ -->

<!-- 帮助 -->

<!-- ================================================ -->

<target name="help" description="显示帮助信息">

<echo message="target 描述" />

<echo

message="-----------------------------------------------------------------" />

<echo message="compile 编译 " />

<echo message="create_code 创建代码" />

</target>

<!-- =============================================== -->

<!-- 编译代码 -->

<!-- =============================================== -->

<target name="compile" description="编译代码">

<echo>编译程序代码</echo>

<javac srcdir="${sources}" destdir="${build.classes}" classpathref="classpath"

debug="${debug}" optimize="${optimize}" deprecation="${deprecation}" />

</target>

<!-- =============================================== -->

<!-- 创建客户端代码 -->

<!-- =============================================== -->

<target name="create_code" description="创建代码">

<echo>创建代码</echo>

<wsgen outputDirectory="${sources}" wsdl="${basedir}/HelloService.wsdl"

package="com.googlepages.smallnest.facet" overwrite="true" />

</target>

</project>

 

下面是另一种BUILDXML

<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />

内容如下:

<?xml version="1.0"?>

<project name="XFireProject" default="genfiles" basedir=".">

    <property name="lib" value="lib" />

    <path id="myclasspath">

        <fileset dir="${lib}">

            <include name="*.jar" /> 

        </fileset> 

        <pathelement location="${genfiles}" />

    </path>

    <!--通过XFire ant任务生成客户端代码的存放位置-->

    <property name="code_path" value="src.client" /> 

    <!--需要生成客户端代码的wsdl文件-->

    <property name="wsdl_path" value=" http://localhost:8080/xfire/services/SayHelloService?wsdl" />

    <!--生成客户端代码的包名-->

    <property name="code_package" value="com.liuxiang.xfire.client" />

   

    <!-- Remove classes directory for clean build -->

    <target name="clean" description="Prepare for clean build"> 

        <delete dir="${code_path}"/>

        <mkdir dir="${code_path}"/>

    </target>

   

    <!--<target name="genfiles" depends="clean" description="Generate the files">  -->

    <target name="genfiles" description="Generate the files">

        <taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="myclasspath" />

        <!--outputDirectory属性定义创建的代码所在的文件夹

            wsdlweb服务的wsdl文件

            package代表创建的代码的package

        -->

        <wsgen outputDirectory="${code_path}" wsdl="${wsdl_path}" package="${code_package}" binding="xmlbeans" /> 

    </target>

</project>

 

XFire Eclipse Plugin    XFireWSDL文档生成代码的插件。该插件需要Eclipse 3.2Java 5

根据WSDL生成客户端代码

 

XFire允许通过运行Ant任务,根据WSDL文件生成访问Web Service的客户端代码存根,同时XFire还提供了一个Eclipse插件完成相同的任务。本节里,我们将学习通过XFire Eclipse插件生成BbtForumService客户端存根代码的知识。

 

安装Eclipse XFire 插件---下面下载的插件JAR包(加到目录下就行了eclipse/plugins

1Help->Software Updates->Find and Install...

 

2.选择“Search for new features to install”,并点击Next

 

3.选择“New Remote Site...”,创建一个NameXFireURL

 

http://dist.codehaus.org/xfire/update/的网站;

 

4.点击Finish安装XFire插件。

 

使用插件创建客户端代码存根

· File->New->Other...->XFire->Code generation from WSDL document

· 弹出一个对话框,如图3所示:

 

指定WSDL文件的位置,存根代码的输出地址及对应的类包,点击Finish

· XFire插件将在生成客户端代码存根的同时生成服务端代码的存根,如下图所示:

 

SayHelloServiceClientSayHelloServicePortType的工厂类,它提供了若干个获取 SayHelloServicePortType实例的重载方法。SayHelloServicePortType对应服务端的窄接口 ISayHello类。而SayHelloServiceImpl是服务端的存根代码,在META-INF中还有XFire的服务配置文件。 对于客户端来说,一般不需要服务端的代码,所以你可以将SayHelloServiceImplMETA-INF删除。

 

下面,我们利用XFire生成的SayHelloServiceClient对服务端的Web Service进行调用:

 

package com.baobaotao.xfire.client;

 

public class StubClient {

 

public static void main(String[] args) {

 

BbtForumServiceClient client = new BbtForumServiceClient();

 

String serviceUrl = "http://localhost:8080/baobaotao/service/BbtForumService";

 

①获取对应服务窄接口实例

 

BbtForumServicePortType portType = client.getBbtForumServiceHttpPort(serviceUrl);

 

int count = portType.getRefinedTopicCount(20);②对服务进行调用

 

System.out.println("count:" + count);

 

}

}

 

出错:---客户端调用远程方法出错

java.lang.NoClassDefFoundError: org/apache/commons/httpclient/Credentials

是因为客户端WEB-INF/lib目录下缺少以下JAR包:

commons-attributes-api-2.1.jarcommons-beanutils-1.7.0.jarcommons-codec-1.3.jarcommons-discovery-0.2.jarcommons-httpclient-3.0.jar

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值