带CXF的java Web服务:如何使用ResponseWrapper?
我们正在使用以下方法创建一个由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);
}
我使用包装器来定义请求resp的元素。更详细的响应:正确的元素名称(以大写字符开头)、必需元素和可选元素,…)。但我不确定这是否是正确的方法(没有关于包装器的深入文档,不是吗?)
类MyResponse:
public class MyResponseWrapper {
private String result;
private ModeEnum status;
// getters and setters
}
MyReposeWrapper类
@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
}
目前,我不明白包装。当我返回MyResponse实例时,如何将MyResponse中的数据分别注入MyResponseWrapper和响应的SOAP主体
通过测试此Web服务,我可以看到MyResponseWrapper的一个实例被实例化,SOAP主体包含正确的元素,但包含默认数据(例如:result=“fu”而不是“Hello”)。我期望CXF将匹配数据从MyResponse注入MyResponseWrapper。这不对吗
如果这样做是错误的: 在使用Java2WS时,Wat是指定生成的SOAP xml的正确方法吗
顺便说一下:上面的源代码片段只是从更复杂(更多字段)的类中选取的示例
# 1 楼答案
这是正确的做法。 请求和响应包装器只允许覆盖请求/响应元素的xml名称空间和元素/属性名称;分别-依次映射到用于管理这些值的方法
参考:http://cxf.apache.org/docs/developing-a-service.html#DevelopingaService-The@RequestWrapperannotation