gpt4 book ai didi

java - 带有 CXF : How to use the ResponseWrapper? 的 Web 服务

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:02:17 25 4
gpt4 key购买 nike

我们正在使用以下方法创建一个由 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);
}

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

MyResponse 类:

public class MyResponseWrapper {

private String result;
private ModeEnum status;

// getters and setters
}

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
}

目前我不了解包装器。当我返回 MyReponse 的实例时,如何将来自 MyResponse 的数据分别注入(inject) MyResponseWrapper 到响应的 SOAP 主体?

通过测试此 Web 服务,我可以看到 MyResponseWrapper 实例已实例化,并且 SOAP 主体包含正确的元素但具有默认数据(例如:result="fu"而不是“Hello”)。我希望 CXF 将匹配数据从 MyResponse 注入(inject)到 MyResponseWrapper。错了吗?

如果这是错误的做法:使用 Java2WS 时指定生成的 SOAP xml 的正确方法是什么?

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

最佳答案

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

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

The @RequestWrapper annotation is defined by the javax.xml.ws.RequestWrapper interface. It is placed on the methods in the SEI. As the name implies, @RequestWrapper specifies the Java class that implements the wrapper bean for the method parameters that are included in the request message sent in a remote invocation. It is also used to specify the element names, and namespaces, used by the runtime when marshalling and unmarshalling the request messages.

The following table describes the properties of the @RequestWrapper annotation.

localName

Specifies the local name of the wrapper element in the XML representation of the request message. The default value is the name of the method or the value of the @WebMethod annotation's operationName property.

targetNamespace

Specifies the namespace under which the XML wrapper element is defined. The default value is the target namespace of the SEI.

className

Specifies the full name of the Java class that implements the wrapper element.

关于java - 带有 CXF : How to use the ResponseWrapper? 的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4231934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com