XFire初识-使用Eclipse开发Web Service

 

前两天下了XFire1.0和Axis2。一直拿不定在项目中使用Axis2还是XFire。那就两个分别作个example测试一番。

首先是XFire:

下载XFire库:
distributionXFire-all,distribution是一些依赖包,例子及文档,XFire-all是XFire的核心包.

然后是根据文档中的Quick Start来实现一个Web Service,Quick Start中举的例子在distibution目录下的example/book下.根据Quick Start来实现一个Web Service的过程是非常简单的,文档上这么讲而且事实也是非常简单的,如果在Eclipse中也就是包括以下几个步骤:
  1. 设置目录结构,设置类路径.
  2. 编写一个要作为Web Service发布的java类.
  3. 创建XFire配置.
  4. 修改web.xml,为XFire配置Servlet.
  5. 因为Lomboz会自动部署,那么剩下的就是测试Web Service部署是否成功了.
 
下面就开始在Eclipse中开始以上列出的步骤:
JDK:JSDK 1.4
Container: Tomcat5.0

1.新建一个dynamic  Web Project 名称为testXFire,添加XFire库(包括XFire-distibution中lib中所有的.jar文件和xfire-all-1.0.jar)到工程中,做完上述工作后整个工程结构如下图:
project-view

2.编写一个需要作为Web Service发布的java类:
 
    a)Service类
  package com.bws.XFireDemo;
   
   public class BookService
   {
       private Book onlyBook;
      
       public BookService()
       {
           onlyBook = new Book();
           onlyBook.setAuthor("Dan Diephouse");
           onlyBook.setTitle("Using XFire");
           onlyBook.setIsbn("0123456789");
       }
   
       public Book[] getBooks()
       {
           return new Book[] { onlyBook };
       }
      
       public Book findBook(String isbn)
       {
           if (isbn.equals(onlyBook.getIsbn()))
               return onlyBook;
          
           return null;
       }
   }
 
    b)对应的Book.java
 package com.bws.XFireDemo;
  
  public class Book {
      private String title;
      private String isbn;
      private String author;
      public String getIsbn()
      {
          return isbn;
      }
  
      public void setIsbn(String isbn)
      {
          this.isbn = isbn;
      }
  
      public String getTitle()
      {
          return title;
      }
  
      public void setTitle(String title)
      {
          this.title = title;
      }
  
      public String getAuthor()
      {
          return author;
      }
  
      public void setAuthor(String author)
      {
          this.author = author;
      }
  }
 
 

3.创建XFire配置:
    a)在源文件夹中创建META-INF/xfire文件夹.
    b)在该文件夹中创建一个service.xml文件,这个文件就是描述需要部署的Web Services.内容如下:
  <beans xmlns="http://xfire.codehaus.org/config/1.0">
    <service>
      <name>BookService</name>
      <namespace>http://com.bws.XFireDemo/BookService</namespace>
      <serviceClass>test.BookService</serviceClass>
    </service>
  </beans>
     name:Web Service名称
     namespace:命名空间
     serviceClass:作为Web Service发布的类名
     完成后目录结构如下图:
XFire Configuration

 
4.修改web.xml,为XFire配置Servlet.
 <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>
 

5.测试Web Service
     a)将XFire所需库拷贝至WebContent/WEB-INF/lib下:
deploy-lib
     b)运行工程于服务器上:
deploy-Run on Server
 
     c)访问 http://localhost:8080/testXFire/services/BookService?wsdl将会显示BookService Web Service的描述文件,其中不久又对服务的描述,并且会自动生成服务引用类的typeMapping即complexType.
<?xml version="1.0" encoding="UTF-8" ?>
- < wsdl:definitions xmlns:wsdl =" http://schemas.xmlsoap.org/wsdl/ " xmlns:ns1 =" http://XFireDemo.bws.com " xmlns:soap11 =" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:soap12 =" http://www.w3.org/2003/05/soap-envelope " xmlns:soapenc11 =" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:soapenc12 =" http://www.w3.org/2003/05/soap-encoding " xmlns:tns =" http://com.bws.XFireDemo/BookService " xmlns:wsdlsoap =" http://schemas.xmlsoap.org/wsdl/soap/ " xmlns:xsd =" http://www.w3.org/2001/XMLSchema " targetNamespace =" http://com.bws.XFireDemo/BookService ">
- < wsdl:types >
- < xsd:schema targetNamespace =" http://com.bws.XFireDemo/BookService " elementFormDefault =" qualified " attributeFormDefault =" qualified ">
- < xsd:element name =" getBooks ">
  < xsd:complexType />
  </ xsd:element >
- < xsd:element name =" getBooksResponse ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element name =" out " type =" ns1:ArrayOfBook " nillable =" true " minOccurs =" 1 " maxOccurs =" 1 " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
- < xsd:element name =" findBook ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element name =" isbn " type =" xsd:string " nillable =" true " minOccurs =" 1 " maxOccurs =" 1 " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
- < xsd:element name =" findBookResponse ">
- < xsd:complexType >
- < xsd:sequence >
  < xsd:element name =" out " type =" ns1:Book " nillable =" true " minOccurs =" 1 " maxOccurs =" 1 " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:element >
  </ xsd:schema >
- < xsd:schema targetNamespace =" http://XFireDemo.bws.com " elementFormDefault =" qualified " attributeFormDefault =" qualified ">
- < xsd:complexType name =" ArrayOfBook ">
- < xsd:sequence >
  < xsd:element name =" Book " type =" ns1:Book " nillable =" true " minOccurs =" 0 " maxOccurs =" unbounded " />
  </ xsd:sequence >
  </ xsd:complexType >
- < xsd:complexType name =" Book ">
- < xsd:sequence >
  < xsd:element name =" author " type =" xsd:string " minOccurs =" 0 " nillable =" true " />
  < xsd:element name =" isbn " type =" xsd:string " minOccurs =" 0 " nillable =" true " />
  < xsd:element name =" title " type =" xsd:string " minOccurs =" 0 " nillable =" true " />
  </ xsd:sequence >
  </ xsd:complexType >
  </ xsd:schema >
  </ wsdl:types >
- < wsdl:message name =" findBookRequest ">
  < wsdl:part element =" tns:findBook " name =" parameters " />
  </ wsdl:message >
- < wsdl:message name =" getBooksResponse ">
  < wsdl:part element =" tns:getBooksResponse " name =" parameters " />
  </ wsdl:message >
- < wsdl:message name =" findBookResponse ">
  < wsdl:part element =" tns:findBookResponse " name =" parameters " />
  </ wsdl:message >
- < wsdl:message name =" getBooksRequest ">
  < wsdl:part element =" tns:getBooks " name =" parameters " />
  </ wsdl:message >
- < wsdl:portType name =" BookServicePortType ">
- < wsdl:operation name =" getBooks ">
  < wsdl:input message =" tns:getBooksRequest " name =" getBooksRequest " />
  < wsdl:output message =" tns:getBooksResponse " name =" getBooksResponse " />
  </ wsdl:operation >
- < wsdl:operation name =" findBook ">
  < wsdl:input message =" tns:findBookRequest " name =" findBookRequest " />
  < wsdl:output message =" tns:findBookResponse " name =" findBookResponse " />
  </ wsdl:operation >
  </ wsdl:portType >
- < wsdl:binding name =" BookServiceHttpBinding " type =" tns:BookServicePortType ">
  < wsdlsoap:binding style =" document " transport =" http://schemas.xmlsoap.org/soap/http " />
- < wsdl:operation name =" getBooks ">
  < wsdlsoap:operation soapAction ="" />
- < wsdl:input name =" getBooksRequest ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:input >
- < wsdl:output name =" getBooksResponse ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:output >
  </ wsdl:operation >
- < wsdl:operation name =" findBook ">
  < wsdlsoap:operation soapAction ="" />
- < wsdl:input name =" findBookRequest ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:input >
- < wsdl:output name =" findBookResponse ">
  < wsdlsoap:body use =" literal " />
  </ wsdl:output >
  </ wsdl:operation >
  </ wsdl:binding >
- < wsdl:service name =" BookService ">
- < wsdl:port binding =" tns:BookServiceHttpBinding " name =" BookServiceHttpPort ">
  < wsdlsoap:address location =" http://localhost:8080/testXFire/services/BookService " />
  </ wsdl:port >
  </ wsdl:service >
</ wsdl:definitions >
 
 
至此Web Service的发布过程就完成了,剩下的就是进行Client的测试了. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值