熟悉 CXF
Apache CXF Web Service Development——Develop and deploy SOAP and RESTful Web Services
Naveen Balani & Rajeev Hathi
2009 Packt Publishing
Apache CXF 是一个业界领先的构建在标准web services之上的开发框架 ,其目标是简化 web services 的开发.
Web service相关标准
XML
SOAP (Simple Object Access Protocol)
SOAP is a protocol for exchanging XML-based messages over a network, typically using HTTP protocol.
WSDL (Web Services Description language)
WSDL 是一种基于XML语言的用于描述 web services 的标准REST (Representational State Transfer)
REST is neither a technology nor a standard; it’s an architectural style—a set of guidelines for exposing resources over the Web(by a Uniform Resource Indicator (URI)).Service Registry
Service Registry 提供了一种查找 web services 的机制.
web services 简介
World Wide Web Consortium (W3C) 定义:
A Web service is a software system identified by a URI whose public interfaces and bindings are defined and described using XML (specifically WSDL). Its definition can be discovered by other software systems. These systems may then interact with the web service in a manner prescribed by its definition, using XML-based messages conveyed by Internet protocols.
Web services are next generation web applications, modules, or components that can be thought of as a
service provided over the web.
A web service can be thought of as a self contained, self describing, modular application that can be published, located, and invoked across the web.
web services 提供的最大好处是交互性: interoperability.
web service开发方法
一个web service包含三种类型的角色—服务消费者 service consumer, 服务提供者 service provider, 和一个可选的服务注册 service registry.
服务提供者通过网络发布(基于SOAP的服务) WSDL文件,使用者可直接或通过查找服务注册来使用此服务。使用者通常利用web service框架提供的工具根据WSDL文件生成客户端代码来与服务进行交互。
在RESTful Web Services中没有正式的服务契约(contract ,即WSDL)。服务请求者需要知道消息的格式,如XML或JSON,以及支持的操作。使用者使用URI通过HTTP协议来启用服务。
REST 的最佳场景是当你通过互联网公开一个公共 API 来对数据的 CRUD 操作进行处理的时候。REST 专注于通过一个一致性接口访问命名资源。
SOAP提供了自己的协议并专注于公开某些应用逻辑(不是数据)为服务。SOAP 公开的是操作。SOAP 专注于访问通过不同的接口实现了某些业务逻辑的命名操作。
Web service SOAP 通信风格
两种SOAP通信方式:Document和RPC。
SOAP 通信方式(message styles)在 WSDL 文档中被定义为 SOAP binding. SOAP binding 可以是编码或文本( encoded use or a literal use).
Document style,将XML文档作为载荷( payloads),这些XML文档遵循定义良好的约定 (well defined contracts), 通常使用 XML schema 定义来创建. 文本文档(Document literal style)是首选的通讯方式。
RPC (Remote Procedure Call) style, 表明 SOAP 内容体中包含一个方法的XML表达。传统上RPC与SOAP编码规则结合使用,该组合被称为RPC /编码(RPC/encoded)。
CXF
选择CXF的原因:
- 支持所有的主流Web Service 标准
- 提供简化的SOAP和RESTful服务编程模型
- 提供多种其他应用协议选项
- 使用简便
- 部署灵活
使用CXF开发WebService
一般过程:
- 新建 Service Endpoint Interface (SEI) 并定义业务方法供服务使用
- 创建实现类,标注其为web service.
- 创建 beans.xml 并使用 JAX-WS frontend定义此服务类为 Spring bean.
SEI
|
@WebService
注解表示此接口为 Service Endpoint Interface,是 JAX-WS 提供的注解之一。
@WebMethod
注解是可选的,用于自定义web服务操作。@WebMethod
提供了operationName, action, exclude三个属性设置(最终反映在WSDL文档中)。
实体类Order可在类上添加@XmlRootElement(name = "Order")
。 其属于Java Architecture for XML Binding (JAXB) 注解。JAXB 注解用于映射XML schema与Java代码。@XmlRootElement
将类Order映射为XML中的根元素,类中的属性默认映射为@XmlElement
.
实现类
|
基于Spring Bean的服务端
|
开发客户端
一般过程:
- 开发
client-beans.xml
来定义客户端的工厂类为使用JAX-WS frontend的Spring bean - 开发客户端Java应用来启用web服务
client-beans.xml
|
- serviceClass:指定 web service SEI
- address:指定SEI发布的URL地址(客户端访问地址)
客户端Java代码
|
Web.xml配置
|
项目结构说明
beans.xml
和web.xml
位于webapp\WEB-INF
文件夹client-beans.xml
位于demo\order\client
文件夹(client类所在目录)
项目运行说明
依赖tomcat。原文依赖ant
CXF架构
• Bus
• Frontend
• Messaging and Interceptors
• Service Model
• Data bindings
• Protocol bindings
• Transport