• Apache CXF入门


      CXF简介

      Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的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)支持多种协议:

        SOAP1.1,1.2

        XML/HTTP

        CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)

      (2)可以与Spring进行快速无缝的整合

      (3)灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。

       看这个之前可以看看关于WebService的文章:链接http://www.cnblogs.com/xiaobai1226/p/7543965.html

      接下来就开始写一个CXF的简单入门程序

      一、服务端程序

      首先去官网下载jar包

      官方下载网址:http://cxf.apache.org/download.html

      进入此页面后,会看到此页面

     

      下载好后会看到这样一个压缩文件:

      解压后,有这些目录文件

      下面简单介绍一下这些目录以及里面文件的作用

       (1)bin:是 CXF 框架中所提供的代码生成、校验、管理控制台工具:

       (2)docs:CXF 所有类(class)对应的 API 文档,为开发者使用 CXF 完成应用开发提供应有的帮助

       (3)etc:包含一个基本的 Service 暴露所需要的 web.xml 文件,及其它的配置文件

       (4)lib:目录中包含 CXF 及其运行时所需要的和可选的第三方支持类包(.jar 文件),可以根据不同项目所需的 CXF 特性选择所需要的支持类包

       (5)licenses:列表了引用第三方 jar 包的相关许可协议

       (6)samples:samples 目录中包含了所有随 CXF 二进制包发布的示例,包含这些示例的源代码和相关 Web 应用配置文件,可以方便地用 Ant 来编译运行测试这些示例,来了解 CXF 的开发和使用的方法。可以通过 samples 目录和它各个子目录下的 README.txt 的文件来详细了解示例的编译与运行的步骤

       (7)LICENSE :文件中包含了 CXF 框架的授权协议 Apache License Version 2.0 。

       (8)NOTICE :罗列了 CXF 框架用到的相关第三方组件的授权协议以其它的相关信息

       (9)README :文件中包含了 CXF 框架本身的一些简要说明。

         (10)release_notes.txt :包含了 CXF 发布时的一些信息,包括运行时所需要的环境,修复 BUG 的列表等

      接下来,第一步:创建动态web项目

      第二步:导入CXF相关jar包,以下jar包是使用CXF所必须的jar包(CXF能运行起来最基本的jar包)

     

      第三步:在web.xml中配置CXF框架提供的一个Servlet

      <!-- 配置CXF框架提供的Servlet -->
      <servlet>
          <servlet-name>cxf</servlet-name>
          <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
          <!-- 通过初始化参数指定CXF框架的配置文件位置 -->
          <init-param>
              <param-name>config-location</param-name>
              <param-value>classpath:cxf.xml</param-value>
          </init-param>
      </servlet>
      <servlet-mapping>
          <servlet-name>cxf</servlet-name>
          <url-pattern>/service/*</url-pattern>
      </servlet-mapping>

      第四步:在类路径下提供cxf.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://cxf.apache.org/bindings/soap 
                        http://cxf.apache.org/schemas/configuration/soap.xsd
                        http://cxf.apache.org/jaxws 
                        http://cxf.apache.org/schemas/jaxws.xsd">
        
    </beans>

      第五步:开发一个接口和实现类

      接口,接口上必须使用@WebService实现类(jdk1.6及以后才支持@WebService注解,所以要确保jdk版本够高)

    import javax.jws.WebService;
    
    @WebService
    public interface TestCXFService {
        public String firstCXF(String name);
    }

      实现类

    public class TestCXFServiceImpl implements TestCXFService{
        @Override
        public String firstCXF(String name) {
            System.out.println("基于CXF开发的服务端firstCXF被调用了");
            return "Hello "+name;
        }
    }

      第六步:在cxf.xml中注册服务

    <bean id="firstCXFService" class="com.cxf.service.impl.TestCXFServiceImpl"/>
    <!-- 注册服务  -->
    <jaxws:server id="myCXFService" address="/cxfService">
        <jaxws:serviceBean>
            <ref bean="firstCXFService"/>
        </jaxws:serviceBean>
    </jaxws:server>

      到现在,使用CXF配置的服务就配置成功了

      接下来,启动tomcat,在浏览器访问这个地址:http://localhost:8080/testCXF/service/cxfService?wsdl

      http://配置此服务的服务器ip地址 / 端口 / 你所创建的工程的名称 / 自己在web.xml配置的路径  / 自己配置的address?wsdl
      注意:(1)由于我使用tomcat所以端口号为8080
           (2)<url-pattern>/service/*</url-pattern>
             

    (3)address="/cxfService"

      若出现这个页面说明成功了

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.cxf.com/" name="TestCXFServiceImplService" targetNamespace="http://impl.service.cxf.com/">
    <wsdl:import location="http://localhost:8080/testCXF/service/cxfService?wsdl=TestCXFService.wsdl" namespace="http://service.cxf.com/"></wsdl:import>
    <wsdl:binding name="TestCXFServiceImplServiceSoapBinding" type="ns1:TestCXFService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="firstCXF">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="firstCXF">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="firstCXFResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="TestCXFServiceImplService">
    <wsdl:port binding="tns:TestCXFServiceImplServiceSoapBinding" name="TestCXFServiceImplPort">
    <soap:address location="http://localhost:8080/testCXF/service/cxfService"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>

       二、客户端程序

      有两种方式:方式一:使用jdk提供的wsimport命令生成本地代码完成调用(另一篇介绍webservice的文章中有使用,链接在文章开头给出了,在这里不再使用)

            方式二:使用CXF提供的方式(我们使用第二种方式)    

      第一步:创建Java项目并导入CXF相关jar包 (jar包同上)

      第二步:使用wsimport或者CXF提供wsdl2java生成本地代码,只需要生成接口文件

      打开cmd进入CXF文件夹的bin目录下(如apache-cxf-3.2.0in),输入

    wsdl2java -d . -p com.cxf.client http://localhost:8080/testCXF/service/cxfService?wsdl

      .代表将生成的文件放到当前目录下,-p代表生成的目录结构,最后的地址代表根据哪个wsdl生成文件

      敲回车,出现以下结果,和方式一不同这个成功后,没有任何提示

      这时就发现,当前目录下出现了这个文件夹

      打开后,有以下目录文件,而我们只需要用到一个接口文件(红框标出来的)

      

      第三步:将接口文件复制到项目中

      

      复制过来后,我们需要将红框内内容删掉,因为我们只把一个接口文件复制过来了,其他的并没有动,所以找不到这个文件,而我们又用不到,所以直接删掉就好  

      删除后接口内代码是这样的

    package com.cxf.client;
    
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.xml.bind.annotation.XmlSeeAlso;
    import javax.xml.ws.RequestWrapper;
    import javax.xml.ws.ResponseWrapper;
    
    /**
     * This class was generated by Apache CXF 3.2.0
     * 2017-09-24T12:35:54.073+08:00
     * Generated source version: 3.2.0
     * 
     */
    @WebService(targetNamespace = "http://service.cxf.com/", name = "TestCXFService")
    @XmlSeeAlso({})
    public interface TestCXFService {
    
        @WebMethod
        @RequestWrapper(localName = "firstCXF", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXF")
        @ResponseWrapper(localName = "firstCXFResponse", targetNamespace = "http://service.cxf.com/", className = "com.cxf.client.FirstCXFResponse")
        @WebResult(name = "return", targetNamespace = "")
        public java.lang.String firstCXF(
            @WebParam(name = "arg0", targetNamespace = "")
            java.lang.String arg0
        );
    }

      第四步:提供spring配置文件,注册客户端代理对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://cxf.apache.org/bindings/soap 
                        http://cxf.apache.org/schemas/configuration/soap.xsd
                        http://cxf.apache.org/jaxws 
                        http://cxf.apache.org/schemas/jaxws.xsd">
        
        <!-- 注册CXF客户端代理对象,通过spring框架创建这个代理对象,使用代理对象实现远程调用 -->
        <jaxws:client id="myCXFClient" address="http://localhost:8080/testCXF/service/cxfService" serviceClass="com.cxf.client.TestCXFService">
            
        </jaxws:client>
    </beans>

      第五步:写一个测试类,读取spring配置文件,创建spring工厂,从工厂中获取代理对象,实现远程调用

    package com.cxf.client;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestCXFClient {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:cxf.xml");
            TestCXFService proxy = (TestCXFService) ctx.getBean("myCXFClient");
            String result = proxy.firstCXF("my first CXFClient");
            System.out.println(result);
        }
    }

      执行main方法(此时要确保服务端启动着),出现以下结果,就证明程序运行成功了

      客户端控制台打印

            

      服务端控制台打印

            

      到现在,CXF入门程序就完成了。

      

      

     

  • 相关阅读:
    C#之时间统计
    Unity之屏幕画线
    Unity之坐标转换
    Unity3d之按键
    Unity3d之截图
    [Windows]A盘凭空消失
    函数的多类型传值
    isdigit()
    函数的返回值
    locals()
  • 原文地址:https://www.cnblogs.com/xiaobai1226/p/7583758.html
Copyright © 2020-2023  润新知