3.使用CXF开发webService

CXF 简介

关于 Apache CXF

Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。目前它仍只是 Apache 的一个孵化项目。

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

功能特性

CXF 包含了大量的功能特性,但是主要集中在以下几个方面:

  1. 支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。
  2. Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了 JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL 优先开发,也支持从 Java 的代码优先开发模式。
  3. 容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。
  4. 支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。

项目目标

下面列出了来自 Apache CXF 官方网站(参见 参考资源)的项目目标。

概要

  • 高性能
  • 可扩展
  • 简单且容易使用

支持多种标准

  • 支持 JAX-WS、 JAX-WSA、JSR-181 和 SAAJ;
  • 支持 SOAP 1.1、1.2、WS-I BasicProfile、WS-Security、WS-Addressing、WS-RM 和 WS-Policy;
  • 支持 WSDL 1.1 、2.0;
  • 支持 MTOM;

多种传输方式、Bindings、Data Bindings 和 Format

  • Bindings:SOAP、REST/HTTP;
  • Data Bndings:目前支持 JAXB 2.0、Aegis 两种,默认是 JAXB 2.0。XMLBeans、Castor 和 JiBX 数据绑定方式将在 CXF 2.1 版本中得到支持;
  • 格式(Format):XML、JSON;
  • 传输方式:HTTP、Servlet、JMS 和 Jabber;
  • 可扩展的 API 允许为 CXF 增加其它的 Bindings,以能够支持其它的消息格式,比如:CSV 和固定记录长度。

灵活部署

  • 轻量级容器:可在 Tomcat 或基于 Spring 的容器中部署 Services;
  • 集成 JBI:可以在如 ServiceMix, OpenESB or Petals 等等的 JBI 容器中将它部署为一个服务引擎;
  • 集成 SCA:可以部署在如 Tuscany 之类的 SCA 容器中;
  • 集成 J2EE:可以在 J2EE 应用服务器中部署 Services,比如:Geronimo、JOnAS、JBoss、WebSphere Application Server 和 WebLogic Application Server,以及 Jetty 和 Tomcat;
  • 独立的 Java 客户端/服务器。

支持多种编程语言

  • 全面支持 JAX-WS 2.0 客户端/服务器编程模型;
  • 支持 JAX-WS 2.0 synchronous、asynchronous 和 one-way API's;
  • 支持 JAX-WS 2.0 Dynamic Invocation Interface (DII) API;
  • 支持 wrapped and non-wrapped 风格;
  • 支持 XML messaging API;
  • 支持 JavaScript 和 ECMAScript 4 XML (E4X) ,客户端与服务端均支持;
  • 通过 Yoko 支持 CORBA;
  • 通过 Tuscany 支持 SCA;
  • 通过 ServiceMix 支持 JBI ;

代码生成

  • Java to WSDL;
  • WSDL to Java;
  • XSD to WSDL;
  • WSDL to XML;
  • WSDL to SOAP;
  • WSDL to Service;

--------------------------------我是分割线---------------------------------------------

上方都是cxf的简介,摘于百度。

以下内容分为几个模块:

1.使用cxf实现webService(还是使用之前的服务端代码发布,客户端代码测试);

2.分析cxf生成的wsdl文档

第一:创建一个java(CXFWebService_server)工程,创建lib文件夹,将相关jar包放入并build path

  主要jar包如下:

    commons-logging-1.1.jar
      geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
      geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
      geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
      geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
      geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
      jaxb-api-2.1.jar
      jaxb-impl-2.1.6.jar
      jaxws-api-2.1.jar
      jetty-6.1.5.jar
      jetty-util-6.1.5.jar
      neethi-2.0.jar
      saaj-api-1.3.jar
      saaj-impl-1.3.jar
      stax-api-1.0.1.jar
      wsdl4j-1.6.1.jar
      wstx-asl-3.2.1.jar
      XmlSchema-1.2.jar
      xml-resolver-1.2.jar

但是为了方便,我直接将整个cxf的jar包都放入lib下,并build path

第二:编写server端代码,同上一篇server端代码

/**SEI及其实现类必须添加@WebService注解*/
@WebService
public interface HelloWS {

    /**SEI方法添加@WebMethod注解*/
    @WebMethod
    public String sayHello(String name);
}
/**SEI实现类添加@WebService注解*/
@WebService
public class HelloWSImpl implements HelloWS {

    /**该方法用于暴露出去,目的是让客户端来调用*/
    /**
     * @param name由客户端传入
     * return String由服务器返回给客户端
     */
    @Override
    public String sayHello(String name) {
        return "hello: "+name;
    }
}
public class RealeaseWS {

    public static void main(String[] args) {
        /**address是发布的地址,只要端口不占用,任意端口*/
        /**端口后可以跟工程名等等,可以随便写*/
        String address = "http://localhost:8989/WebService_Server";
        /**
         * 使用由Endpoint来发布,这里有两个参数:
         * 参数一为发布地址,参数二为SEI接口的实现类对象
         */
        Endpoint.publish(address, new HelloWSImpl());
        /**如果发布成功,打印*/
        System.out.println("webService发布成功!");
    }
}

第三:发布成功后,创建另一个java工程CXFWebService_client,用于生成客户端代码

使用cxf生成客户端代码,这里已经不使用jdk来生成客户端代码了!!!

1.在cxf压缩包中,找到bin文件夹中的wsdl2java.bat,将该文件所在路径加入到环境变量(path)中去;

3.使用CXF开发webService

2.打开cmd控制台窗口,定位到该工程的src目录下

先定位到指定盘符,比如我的src目录在D盘,那么控制台输入D:,回车,就定位到D盘了;

接着定位到src目录cd D:developUtilworkspaceCXFWebService_serversrc

最后输入wsdl2java http://localhost:8989/WebService_Server?wsdl生成客户端文件,生成文件如下:

3.使用CXF开发webService

3.测试程序

public class TestWS {

    public static void main(String[] args) {
        HelloWSImplService factory = new HelloWSImplService();
        HelloWS helloWS = factory.getHelloWSImplPort();
        String result = helloWS.sayHello("webService developed by cxf");
        System.out.println(result);
    }
}

第四:wsdl文档说明

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webService.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloWSImplService" targetNamespace="http://webService.cxf.com/">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webService.cxf.com/" elementFormDefault="unqualified" targetNamespace="http://webService.cxf.com/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
  </wsdl:types>
  <!-- message:定义了消息结构体,分为请求消息和响应消息 -->
  <!-- 响应消息 -->
  <wsdl:message name="sayHelloResponse">
    <wsdl:part element="tns:sayHelloResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <!-- 请求消息 -->
  <wsdl:message name="sayHello">
    <wsdl:part element="tns:sayHello" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <!-- portType:定义了SEI,即服务端的SEI接口 -->
  <wsdl:portType name="HelloWS">
      <!-- operation:代表操作的意思,即表示SEI接口内的方法 -->
    <wsdl:operation name="sayHello">
      <!-- 指定客户端传给服务端的数据 -->
      <wsdl:input message="tns:sayHello" name="sayHello">
    </wsdl:input>
      <!-- 指定服务端返回给客户端的响应数据 -->
      <wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <!-- binding name:表示服务端SEI的实现   type:表示该实现类的类型是服务端的SEI -->
  <wsdl:binding name="HelloWSImplServiceSoapBinding" type="tns:HelloWS">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <!-- 表示服务端SEI实现类方法 -->
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <!-- 指定客户端传给服务端的数据 -->
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <!-- 指定服务端返回给客户端的响应数据 -->
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <!-- 
          name:一个webService容器 
          port:表示入口的意思,指向上方SEI的实现
              意思是:HelloWSImplService对象调用HelloWSImplPort后返回服务端SEI接口实现类对象
          address:服务的发布的url
  -->
  <wsdl:service name="HelloWSImplService">
    <wsdl:port binding="tns:HelloWSImplServiceSoapBinding" name="HelloWSImplPort">
      <soap:address location="http://localhost:8989/WebService_Server"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>