Apache CXF Web Service Development笔记 01

熟悉 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 Vs SOAP,Soap 和 Rest 的区别

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

package demo.order;
import javax.jws.WebService;
@WebService
public interface OrderProcess {
@WebMethod
String processOrder(Order order);
}

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

实现类

package demo.order;
@WebService
public class OrderProcessImpl implements OrderProcess {
public String processOrder(Order order) {
String orderID = validate(order);
return orderID;
}
private String validate(Order order) {
...
}
}

基于Spring Bean的服务端

<!-- beans.xml -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- core components of CXF -->
<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" />
<!-- publish the web service -->
<jaxws:endpoint id="orderProcess" implementor="demo.order.OrderProcessImpl" address="/OrderProcess" />
</beans>

开发客户端

一般过程:

  • 开发 client-beans.xml 来定义客户端的工厂类为使用JAX-WS frontend的Spring bean
  • 开发客户端Java应用来启用web服务

client-beans.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="orderClient" serviceClass="demo.order.OrderProcess" address="http://localhost:8080/orderapp/OrderProcess" />
</beans>
  • serviceClass:指定 web service SEI
  • address:指定SEI发布的URL地址(客户端访问地址)

客户端Java代码

package demo.order.client;
public final class Client {
public Client() {}
public static void main(String args[]) throws Exception {
// START SNIPPET: client
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "demo/order/client/client-beans.xml" });
//获得 SEI
OrderProcess client = (OrderProcess) context.getBean("orderClient");
// Populate the Order bean
Order order = new Order();
//... //set order属性
//使用SEI调用web服务
String orderID = client.processOrder(order);
String message = (orderID == null) ?"Order not approved" : "Order approved; order ID is " + orderID;
System.out.println(message);
System.exit(0);
}

Web.xml配置

<web-app>
...
<context-param>
<param-name>contextConfigLocation</param-name>
44<param-value>WEB-INF/beans.xml</param-value>
4</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

项目结构说明

  • beans.xmlweb.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

文章目录
  1. 1. 熟悉 CXF
    1. 1.1. Web service相关标准
    2. 1.2. web services 简介
    3. 1.3. web service开发方法
    4. 1.4. Web service SOAP 通信风格
    5. 1.5. CXF
    6. 1.6. 使用CXF开发WebService
      1. 1.6.1. SEI
      2. 1.6.2. 实现类
    7. 1.7. 基于Spring Bean的服务端
      1. 1.7.1. 开发客户端
        1. 1.7.1.1. client-beans.xml
        2. 1.7.1.2. 客户端Java代码
      2. 1.7.2. Web.xml配置
        1. 1.7.2.1. 项目结构说明
        2. 1.7.2.2. 项目运行说明
    8. 1.8. CXF架构
|