webService-apache CXF示例

  1. 下载开发包   

    官网:cxf.apache.org

  2. 简单demo 

    第一步:创建动态web项目

    第二步:导入CXF相关jar包

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

      <!-- 配置CXF框架提供的Servlet -->   ctrl + shift + t 找 CXFservlet  可找到完整类路径

      <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>


  4. 第四步:根据classpath配置的路径  在 源码包下提供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">

                <!-- 引入CXF Bean定义如下,早期的版本中使用 -->

                <import resource="classpath:META-INF/cxf/cxf.xml"/>

                <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>

                <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    </beans>


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

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


客户端的开发

注意 :  客户端的调用和 服务端无技术联系 (及不用关心服务端使用的是语言 或是什么框架)

  • l  方式一:使用jdk提供的wsimport命令生成本地代码完成调用


  • l  方式二:使用CXF提供的方式(重点)

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

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



第三步:将接口文件复制到项目中   只需复制接口文件即可(有个xxx.class的地方报错,直接删除即可)


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

        

        其中的address是 访问服务的wsdl的网址  去掉最后的?wsdl即可。

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

        


附录: 

 cxf访问路径的详细介绍: 


服务端配置 图解: 

 1.  在web.xml中 配置cxf提供的servlet -->


2.  cxf服务端 cxf.xml文件的配置(注册服务):


客户端配置图解:

1.客户端spring配置文件的配置 内容及 客户端调用的示例:




猜你喜欢

转载自blog.csdn.net/fanbaodan/article/details/81032827
今日推荐