Web服务与CXF:如何使用ResponseWrapper?

Ber*_*els 10 java web-services cxf response wrapper

我们正在使用以下方法创建一个由java类(Java2WS)驱动的Web服务(基于CXF):

  @WebMethod
  @RequestWrapper(className = "com.myproject.wrapper.MyRequestWrapper")
  @ResponseWrapper(className = "com.myproject.wrapper.MyResponseWrapper")
  public MyResponse verifyCode(@WebParam(name = "code") String code) {
    ...
    return new MyResponse("Hello",StatusEnum.okay);
  }
Run Code Online (Sandbox Code Playgroud)

我使用包装器来定义请求resp的元素.更详细的响应:正确的元素名称(以大写字符开头),必需元素和可选元素,...).但我不确定这是否是正确的方法(没有关于包装器的深入文档,不是吗?)

MyResponse类:

public class MyResponseWrapper {

  private String result;   
  private ModeEnum status;

  // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

MyReponseWrapper类

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myResponse")
public class MyResponseWrapper {

  @XmlElement(name="Result")
  private String result;

  @XmlElement(name = "Status")
  private StatusEnum status;

  public MyResponseWrapper() {
    result="fu"; // just for testing
  }

  // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

目前我不了解Wrappers.当我返回MyReponse的一个实例时,MyResponse的数据如何被分别注入MyResponseWrapper到响应的SOAP主体?

通过测试这个Web服务,我可以看到MyResponseWrapper的实例被实例化,SOAP主体包含正确的元素,但是包含默认数据(例如:result ="fu"而不是"Hello").我预计CXF会将MyResponse的匹配数据注入MyResponseWrapper.那是错的吗?

如果这是错误的方法:在使用Java2WS时,Wat是指定生成的SOAP xml的正确方法吗?

顺便说一下:上面的源代码片段只是从我们更复杂(更多字段)类中获取的示例.

Dar*_*gue 1

这是正确的做法。请求和响应包装器仅允许覆盖请求/响应元素的 xml 命名空间和元素/属性名称;分别 - 依次映射到用于管理这些值的方法。

参考: http: //cxf.apache.org/docs/developing-a-service.html#DevelopingaService-The@RequestWrapperannotation

@RequestWrapper 注释由 javax.xml.ws.RequestWrapper 接口定义。它被放置在 SEI 中的方法上。顾名思义,@RequestWrapper 指定 Java 类,该类为远程调用中发送的请求消息中包含的方法参数实现包装器 bean。它还用于指定运行时在编组和解组请求消息时使用的元素名称和命名空间。

下表描述了@RequestWrapper注释的属性。

本地名称

指定请求消息的 XML 表示形式中包装器元素的本地名称。默认值是方法的名称或 @WebMethod 注释的 operationName 属性的值。

目标命名空间

指定定义 XML 包装器元素的命名空间。默认值是 SEI 的目标命名空间。

班级名称

指定实现包装元素的 Java 类的全名。