Apache CXF入门

@(WebService)[WebService, CXF,wsdl, soap,uddi]

  • Apache CXF入门

    • Apache CXF基本概述
    • Apache CXF框架基本概念
      • Bus总线
      • Frontend前端
      • Message and intercetpors消息和拦截器
      • Service Model服务模型
      • Data binding数据绑定
      • Protocol Bindings协议绑定
      • Transport数据传输
    • Apache CXF入门案例
      • 服务端代码

        • Maven导入CXF框架相关依赖包
        • 在webxml文件中配置CXFServlet
          • 配置文件由cxf框架加载
          • 配置文件由Spring框架加载
        • 提供CXF框架的配置文件
        • 在配置文件中配置服务
        • 访问服务地址
      • 客户端代码
        • 客户端使用方式

Apache CXF基本概述

Apache CXF是一个开源的,全功能的,容易使用的Web服务框架。CXF是两个项目的结合:由IONA技术公司(现在是Progress的一部分)开发的Celtix和由Codehaus主持的团队开发的XFire,合并是由人们在Apache软件基金会共同完成的。CXF的名字来源于”Celtix”和”XFire”的首字母。

CXF的关键的设计考虑因素包括:

  • 前端,如JAX-WS,与核心代码的彻底分离。
  • 简单易用,例如,创建客户端和端点不需标注。
  • 高性能,最少的计算开销。
  • 可嵌入的Web服务组件:例如可以嵌入到Spring Framework和Geronimo中。
    ——参考《维基百科》

官网:Apache CXF官方网站

Apache CXF框架基本概念

Bus(总线)

Bus是CXF框架的支撑,CXF Bus由spring配置文件构成(cxf.xml),在servlet初始化时,通过SpringBusFactory加载,它为所有终端定义了公共的上下文。它织入了所有运行时的结构组件并提供了一个公共的应用上下文。SpringBusFactory扫描并加载类路径
下的META-INF/cxf目录并从以下文件构建上下文。

META-INF/cxf/cxf.xml
META-INF/cxf/cxf-extension.xml
META-INF/cxf/cxf-property-editors.xml

XML文件是安装CXF类库的一部分。CXF内部使用Spring配置。cxf.xml文件中bus定义如下:

<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" />

核心bus组件是CXFBusImpl,该类更多扮演拦截器供应商的角色对于终端出入的请求。这些拦截器一旦应用,即可用于上下文中所有的终端。cxf.xml文件也定义了其他的组件,比如BindingFactoryManager,ConduitFactoryManager等等。这些组件可用于bus的扩展。可以使用getExtension()方法获取这些组件。注册这些组件以用于获取或更新服务终端级别参数如服务绑定,传输协议,中转等等。

Frontend(前端)

CXF提供了前端建模的概念,可以使用不同的前端API创建ws。这些API使用简单工厂bean和JAX-WS实现创建ws。可以创建动态ws客户端。CXF支持的前端主要是JAX-WS。

Message and intercetpors(消息和拦截器)

CXF框架中重要的组件之一是拦截器组件。拦截器拦截客户端和服务器间的消息。在CXF,这通过拦截器链的概念实现。拦截器链是CXF运行时的核心功能。链中的每个拦截器是可配的,用户可以控制他的执行。

框架的核心的是拦截器接口,它定义了两个方法handleMessage和handleFault,都携带Message类型作为参数。

拦截器通常分组成各阶段。每个阶段执行特定的消息处理,每个阶段又被添加到拦截器链中。典型的ws终端有三个连接器链:

Inbound messages chain(入消息链)
Outbound messages chain(出消息链)
Error messages chain(错误消息链)

CXF提供了一些内置拦截器如日志,安全,当然你也可以自定义拦截器。

Service Model(服务模型)

Service Model主要用于生成wsdl文件的各个元素。建模service,创建各种WSDL元素如操作,绑定,终端,schema等等。

Data binding(数据绑定)

Data binding是ws开发的关键。它意味着java对象和xml元素之间的映射。数据绑定组件执行这一工作。CXF使用JAXB2.1。JAXB使用注解定义java对象和XML之间的映射.如下:

@XmlRootElement(name="processOrder", namespace="http://localhost/orderprocess")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name="processOrder", namespace="http://localhost/orderprocess")
public class OrderProcess {@XmlElement(name="arg0", namespace="")private order.Order arg0;//Gettter and Setter…
}

Protocol Bindings(协议绑定)

CXF支持以下形式的绑定:
- SOAP1.1
- SOAP1.2
- CORBA
- Pure XML

Transport(数据传输)

CXF支持以下数据传输方式:
- HTTP
- CORBA
- JMS
- LOCAL

Apache CXF入门案例

该案例是基于CXF和Spring框架以及Maven构建的。

服务端代码

Maven导入CXF框架相关依赖包

<properties><spring.version>4.1.3.RELEASE</spring.version><cxf.version>2.4.2</cxf.version><junit.version>4.10</junit.version>
</properties><dependencies><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency>
</dependencies>

在web.xml文件中配置CXFServlet

配置文件由cxf框架加载
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>CXFTest</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>index.html</welcome-file></welcome-file-list><servlet><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><!-- 配置文件路径配置,可以使用默认值 "/WEB-INF/cxf-servlet.xml"--><init-param><param-name>config-location</param-name><param-value>classpath:cfx-conf.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping>
</web-app>
  • CXF配置文件加载原理
配置文件由Spring框架加载
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>CXFTest</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file><welcome-file>index.html</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:cfx-conf.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>cxf</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class></servlet><servlet-mapping><servlet-name>cxf</servlet-name><url-pattern>/service/*</url-pattern></servlet-mapping></web-app>

提供CXF框架的配置文件`

<?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.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"></beans>

PS:CXF的配置文件由于在web.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.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><!-- 创建入门服务bean -->         <bean id="helloCXFService" class="com.pc.cxf.server.impl.HelloCXFServiceImpl"/><!-- 注册Web Service服务,注册地址为/hello --><jaxws:server id="cxfService" address="/hello"><!-- 注册服务bean --><jaxws:serviceBean><ref bean="helloCXFService"/></jaxws:serviceBean></jaxws:server>
</beans>

访问服务地址

PS:出现这个页面说明前面的配置都成功了,可以进行生成客户端代码了。

客户端代码

客户端使用方式

  • 方式一:使用wsdl2java命令生成的客户端代码调用

命令为:wsdl2java -p com.pc.cxf.client http://localhost:8080/CXFDemo/service/hello?wsdl

在cxf中,也提供了一个用于生成客户端调用代码的工具。它的功能就如同wsimport一样。
此工具位于{cxf_home}/bin目录下。参数与wsimport有所不同。
它包含以下参数:
- -d : 指定代码生成的目录。
- -p : 指定生成的新的包结构。
- (wsdlurl)http://server:port/service?wsdl,必须的参数。

PS:之后的使用方式和《Web Service入门》中一样,这里不再赘述。

  • 方式二:在配置文件中注册代理对象调用

    • 第一步:使用wsdl2java命令生成客户端代码

      • 这里只需要接口文件HelloCXFService.java

PS:需要删除@XmlSeeAlso({ObjectFactory.class})这条注解,否则会报错。

  • 第二步:创建一个项目,并导入和服务端一样的jar包
  • 第三步:将接口文件引入
  • 第四步:复制服务端提供的CXF框架配置文件
  • 第五步:重命名为applicationContext.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.xsdhttp://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><!-- 注册一个代理对象,这个对象是由spring框架注入的,通过这个代理对象可以实现远程调用 --><jaxws:client id="cfxClient" address="http://localhost:8080/CXFDemo/service/hello" serviceClass="com.pc.cxf.client.HelloCXFService"/>
</beans>
  • 第七步:编写测试类并测试
package com.pc.cxf.client.test;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.pc.cxf.client.HelloCXFService;/*** 测试CXF服务* * @author Switch* @data 2016年12月22日* @version V1.0*/
public class HelloCXFServiceTest {ApplicationContext context = null;@Beforepublic void init() {context = new ClassPathXmlApplicationContext("applicationContext.xml");}@Testpublic void testService() {// 获取代理对象HelloCXFService proxy = context.getBean("cfxClient", HelloCXFService.class);// 调用方法System.out.println(proxy.helloCXF("Switch"));System.out.println(proxy.byeCXF());}
}

GitHub:CXFDemo

Apache CXF入门相关推荐

  1. cxf restful_使用Apache CXF开发RESTful服务

    cxf restful 介绍 如您所知,有两种开发Web服务的方法 简单对象访问协议(SOAP) 代表性状态转移(REST) 在继续学习如何使用Apache CXF创建基于REST的Web服务之前,我 ...

  2. cxf 服务端soap报文_使用Apache CXF开发SOAP Web服务

    cxf 服务端soap报文 在上一篇文章中,我逐步介绍了使用apache CXF开发简单的RESTFull服务的步骤. 在本文中,我将讨论使用CXF开发SOAP Web服务. 在继续前进之前,让我们先 ...

  3. 使用Apache CXF开发RESTful服务

    介绍 如您所知,有两种开发Web服务的方法 简单对象访问协议(SOAP) 代表性状态转移(REST) 在继续学习如何使用Apache CXF创建基于REST的Web服务之前,我们将了解什么是REST. ...

  4. 使用Apache CXF开发SOAP Web服务

    在上一篇文章中,我逐步介绍了使用apache CXF开发简单的RESTFull服务的步骤. 在本文中,我将讨论使用CXF开发SOAP Web服务. 在继续前进之前,让我们了解构成SOAP Web服务的 ...

  5. CXF入门教程(一)

    CXF 简介 Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CX ...

  6. Apache CXF 简介

    Apache CXF 简介 开放源代码的服务框架 本教程介绍了 Apache CXF 服务框架的基本知识,并通过讲解自带的例子来初步体验通过 CXF 进行服务的发布与消费:然后搭建基于 Eclipse ...

  7. 使用Apache CXF开发REST风格的WebService

    使用Apache CXF开发REST风格的WebService REST的概述: REST(表现层状态转化),是一种新的软件架构风格,它以资源为核心,使用http,url,xml以及html等流行协议 ...

  8. 解决Apache CXF 不支持传递java.sql.Timestamp和java.util.HashMap类型问题

    在项目中使用Apache开源的Services Framework CXF来发布WebService,CXF能够很简洁与Spring Framework 集成在一起,在发布WebService的过程中 ...

  9. 使用Apache cxf 和Spring在Tomcat下发布Webservice指南

    转载 http://blog.csdn.net/zhangzhaokun/article/details/4750021 最近学习了如何使用apache cxf和Spring发布webservice, ...

最新文章

  1. Spring Boot整合模板引擎jsp
  2. 元气骑士机器人修好后怎么用_《元气骑士》五大“难度”挑战,从手速到恶搞很嗨,还能解锁皮肤...
  3. 计算机四级网络工程师考点速查,全国计算机等级考试标准教程:四级网络工程师...
  4. Android Textview控件
  5. c++访问控制说明符
  6. React中父子组件之间的通信
  7. Perhaps you are running on a JRE rather than a JDK? 关于 idea maven 缓存的问题 清理 idea maven 缓存
  8. pandas 按照固定的列顺序排序,并补足缺失列
  9. mysql中的各种函数(日期函数、字符串函数、数学函数...)
  10. Something about mvss
  11. 塔尖上的AI医疗,它要飞起来必须迈过这几道坎丨Xtecher 观察
  12. KETTLE教程:转换
  13. Java 中的三大特性(超详细篇)
  14. BIN文件和HEX文件差异
  15. word 编号圆圈里面带个数字的输入
  16. fastposter 2.1.1 紧急版本发布 电商级海报生成器
  17. 《全心全意地投入》——英文小译二【英文短篇正能量】
  18. 微信公众号只能设置两个网页授权域名的解决方案
  19. HashMap和Iterator迭代器的小用法
  20. 一文带你学会python新年倒计时

热门文章

  1. Linux下Apache安装与配置(详细步骤+代码+验证)
  2. 经典面试题之 TCP三次握手 和 TCP四次挥手过程----详解
  3. C语言,期末复习之穷举法鸡兔同笼问题
  4. C# web项目中sql数据库转sqlite数据库
  5. webpack -- 无法将“webpack”项识别为 cmdlet 。。。
  6. 【Python】如何在python中执行另一个py文件
  7. sqlalchemy通过已经存在的表生成model的方法
  8. 如何优化Jupyter Notebook
  9. php为什么要提前定义变量
  10. 在图像中隐藏数据:用 Python 来实现图像隐写术