gpt4 book ai didi

java - 为什么 JAX-WS 需要包装类?

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:50 26 4
gpt4 key购买 nike

我创建了一个简单的 web 服务,它实现了一个添加操作,并使用 wsimport 创建了一些客户端文件。现在我想包含尽可能少的 wsdl 特定工件。以下是如何调用 Web 服务的示例:

String serviceNamespace = "http://jws.samples.geronimo.apache.org/";
String serviceName = "CalculatorServiceService";
QName serviceQN = new QName(serviceNamespace, serviceName);
Service service = Service.create(new URL("http://localhost:8080/WebService/calculator?wsdl"), serviceQN);

String portNamespace = "http://jws.samples.geronimo.apache.org/";
String portName = "CalculatorServicePort";
QName portQN = new QName(portNamespace, portName);
Calculator myProxy = (Calculator) service.getPort(portQN, Calculator.class);

但似乎我必须为每条消息包含包装器类。例如添加操作的结果消息:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "addResponse", propOrder = { "_return" })
public class AddResponse {
@XmlElement(name = "return")
protected int _return;
public int getReturn() {
return _return;
}
public void setReturn(int value) {
this._return = value;
}
}

这些包装器在服务接口(interface)的注释中使用:

@WebService(name = "Calculator", targetNamespace = "http://jws.samples.geronimo.apache.org/")
public interface Calculator {
@WebMethod
@RequestWrapper(className = "org.example.webservices.clients.dynamicproxy.Add")
@ResponseWrapper(className = "org.example.webservices.clients.dynamicproxy.AddResponse")
public int add(
@WebParam(name = "value1", targetNamespace = "")
int value1,
@WebParam(name = "value2", targetNamespace = "")
int value2);
}

如果注释被删除,Web 服务将不会运行。

com.sun.xml.ws.model.RuntimeModelerException: runtime modeler error: Wrapper class org.example.webservices.clients.dynamicproxy.jaxws.Add is not found. Have you run APT to generate them?

但为什么我需要那些包装器? JAX-WS 不能即时创建这些包装器吗?您是否看到任何无法从 wsdl 文件中检索到的信息?

最佳答案

默认情况下,您的服务是 WRAPPED,而不是 BARE,因此消息中的顶级项目必须是与操作同名的类型。在“经典”JAX-WS 中,这需要您添加包装器类型。

如果您使用 Apache CXF ,它将使用 ASM 自动生成这些包装器。

关于java - 为什么 JAX-WS 需要包装类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3708631/

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