---
title: "CXF详解二"
author: "ootabc"
date: "2010-12-31T11:16:06Z"
tags: ["SOAP", "Web", "WebService", "XML", "编程"]
categories: ["企业架构"]
slug: "cxf"
draft: false
lastmod: "2010-12-31T11:22:04Z"
id: 856660
url: "https://ootabc.iteye.com/blog/856660"
---

# CXF之JAX-WS

JAX-WS规范是一组XML web services的JAVA API。JAX-WS允许开发者可以选择RPC-oriented或者message-oriented 来实现自己的web services。在 JAX-WS中，一个远程调用可以转换为一个基于XML的协议例如SOAP。在使用JAX-WS过程中，开发者不需要编写任何生成和处理SOAP消息的代码。JAX-WS的运行时实现会将这些API的调用转换成为对于SOAP消息。

在服务器端，用户只需要通过Java语言定义远程调用所需要实现的接口SEI (service endpoint interface)，并提供相关的实现，通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。

在客户端，用户可以通过JAX-WS的API创建一个代理（用本地对象来替代远程的服务）来实现对于远程服务器端的调用。

通过web service所提供的互操作环境，我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作。

## JAX-WS annotation

 主要常用接口：

[Oneway](http://download.oracle.com/javaee/6/api/javax/jws/Oneway.html "annotation in javax.jws") 

只有输入值，无返回值

[WebMethod](http://download.oracle.com/javaee/6/api/javax/jws/WebMethod.html "annotation in javax.jws")

对方法进行注解

[WebParam](http://download.oracle.com/javaee/6/api/javax/jws/WebParam.html "annotation in javax.jws")

对输入参数进行注解

[WebResult](http://download.oracle.com/javaee/6/api/javax/jws/WebResult.html "annotation in javax.jws")

对返回数据进行注解

[WebService](http://download.oracle.com/javaee/6/api/javax/jws/WebService.html "annotation in javax.jws")

注解一个web服务

@WebService，必选的标注。用于导出的服务接口及其实现类

name

定义导出的服务接口的名字，对应于WSDL文档中wsdl:portType。默认是服务接口的Java类名加PortType

targetNamespace

定义导出的服务接口的名域(namespace)，默认是倒置的服务接口Java包名。如demo.cxf.UserService的名域将会是[http://cxf.demo/](http://cxf.demo/)

serviceName

定义服务名，与名域一起唯一标识一个服务。默认是其Java类名

wsdlLocation

其WSDL文档URL。可由服务器容器自动产生

endpointInterface

指定服务接口的Java类。通常用于服务实现类的标注。应当指定类的全名，如demo.cxf.UserService

portName

对应WSDL文档中的wsdl:port元素。默认是Java类名加Port

@WebMethod，可选的标注，用于服务接口的方法

operationName

指定方法在WSDL文档中的名字，客房端用此名字调用方法

action

Specifies the value of the soapAction attribute of the soap:operation element generated for the method. The default value is an empty string.

exclude

生成WSDL文档时将该方法排除在外

@SOAPBinding，可选的标注，用于指定生成的SOAP定义文档风格。关于此标注再详细的信息请查阅SOAP标准等参考资料

style

Style.DOCUMENT (默认)

Style.RPC

SOAP消息风格

use

Use.LITERAL (默认)

Use.ENCODED

SOAP数据编码方式

parameterStyle

ParameterStyle.BARE

ParameterStyle.WRAPPED (默认)

Specifies how the method parameters, which correspond to message parts in a WSDL contract, are placed into the SOAP message body. A parameter style of BARE means that each parameter is placed into the message body as a child element of the message root. A parameter style of WRAPPED means that all of the input parameters are wrapped into a single element on a request message and that all of the output parameters are wrapped into a single element in the response message. If you set the style to RPC you must use the WRAPPED parameter style.

@RequestWrapper，可选的标注，用于指定如何包装客户端调用服务方法使用的参数

@ResponseWrapper，可选的标注，用于指定如何包装客户端调用服务方法的返回值

@WebFault，可选的标注，用于注解服务接口方法抛出的异常

name

异常的名字

targetNamespace

对应的名域，默认是服务接口的名域

faultName

实现该异类的类名

@WebParam，可选的标注，用于指定方法参数的使用方式

name

在WSDL文档中的名字，默认是arg0,arg1…

targetNamespace

对应的名域。默认是服务接口的名域

mode

Mode.IN (默认)、Mode.OUT、Mode.INOUT

对于Java程序没有意义

header

true或者false（默认），指定该参数是否在SOAP消息头部发送

partName

Specifies the value of the name attribute of the wsdl:part element for the parameter when the binding is document.

@WebResult，可选的标注，用于指定返回值的使用方式

name

返回值在WSDL文件中的名字。默认是return

targetNamespace

对应的名域。默认是服务接口的名域

header

true或者false（默认），指定该参数是否在SOAP消息头部发送

partName

Specifies the value of the name attribute of the wsdl:part element for the parameter when the binding is document.

具体请参考j2EE API是如下两个包：

²  [javax.jws](http://download.oracle.com/javaee/6/api/javax/jws/package-frame.html)

²  [javax.jws.soap](http://download.oracle.com/javaee/6/api/javax/jws/soap/package-frame.html)

## JAX-WS Dispatch API

客户端开发者可以使用JAX‐WS 规范中定义的javax.xml.ws.Dispatch API。这是一个低层API，它要求客户端将消息或消息的有效负载（payload）构造成XML，并且要求精通期望的信息或负载的结构。这在客户端希望操作XML 消息层时非常有用。

可以通过调用Service 接口的任何一个createDispatch(...) 方法来获得javax.xml.ws.Dispatch 的实例。

**1、Dispatch对象有两个使用模型：**

l  消息模型

l  负载模型

ü  **消息模型**

在消息模型中，一个Dispatch对象要使用一个完整的消息。完整的消息包括头与包装器。

为Despatch对象指定使用消息模型，当创建Dispatch对象时，要提供java.xml.Service.Mode.MESSAGE值。

ü  **负载模型**

在负载模型中，Dispatch对象仅使用消息负载(消息体body)。

为Despatch对象指定使用负载模型，当创建Dispatch对象时，要提供java.xml.ws.Service.Mode.PAYLOAD值.

**2 、Data Types(数据类型)**

Dispatch对象，因为它们是低级别的对象，使用相同的JAXB生成的高级别的消费者API是不被优化的。

Dispatch对象使用下面几个类型：

l  \`

l  javax.activation.DataSource

l  JAXB

**3、参考：**

²  [http://blog.csdn.net/fhd001/archive/2010/08/01/5780840.aspx](http://blog.csdn.net/fhd001/archive/2010/08/01/5780840.aspx)

²  [http://man.lupaworld.com/content/develop/JDK\_6.0\_API\_html\_zh\_CN/html/zh\_CN/api/javax/xml/ws/class-use/Dispatch.html](http://man.lupaworld.com/content/develop/JDK_6.0_API_html_zh_CN/html/zh_CN/api/javax/xml/ws/class-use/Dispatch.html)