gpt4 book ai didi

wsdl - Axis wsdl 生成

转载 作者:行者123 更新时间:2023-12-01 06:49:57 27 4
gpt4 key购买 nike

我正在使用 Axis 为示例 WebService 建模。我现在正在做的是试图了解自动化 wsdl 和代码生成的局限性。

现在对于一些服务器端代码:

这是示例 Web 服务的框架:

public class TestWebService {
public AbstractAttribute[] testCall( AbstractAttribute someAttribute ) {
....

和我的数据类: 公共(public)抽象类 AbstractAttribute { 字符串名称;

  /*get/set for name*/
public abstract T getValue();
public abstract void setValue(T value);
}

public class IntAttribute extends AbstractAttribute<Integer> {
Integer value;
public Integer getValue(){ return value; }
public void setValue(Integer value){ this.value = value; }
}

public class StringAttribute extends AbstractAttribute<String> {
String value;
/* ok, you got the point, get/set for value field */
}

Axis2 的 eclipse 工具非常乐意从这些来源生成 wsdl,包括属性类的模式,即:

<xs:complexType name="AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="IntAttribute">
<xs:complexContent>
<xs:extension base="xsd:AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:int"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="StringAttribute">
<xs:complexContent>
<xs:extension base="xsd:AbstractAttribute">
<xs:sequence>
<xs:element minOccurs="0" name="value" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>

现在,如果在这里看到一些奇怪的东西,AbstractAttribute 没有 ** abstract="true"** 属性,并且定义了一个 anyType 值元素,它在 IntAttribute 和 StirngAttribute 中被重写。我什至不知道这是否是一个合法的模式(顺便说一句,我认为它不合法)。

更多,如果我尝试从这个 wsdl 生成一个客户端(总是使用 eclipse 工具),生成的源将不会编译,因为 AbstractAttribute 定义了一个

Object localValue;

字段和 Int/String 属性定义

int localValue;

String localValue;

..我试图“容纳”源(显然没有太多希望),结果是服务器尝试实例化一个 AbstractAttribute 实例(抛出 InstantiationException)。

所以我的问题是:有一种方法可以像上面的数据模型一样对某些东西进行建模,或者 Web 服务和 XML 模式一般不是用于这种特定情况的最佳工具?

最佳答案

为了解释您遇到的问题,考虑调用您的服务时 Axis 需要做什么会有所帮助。

Axis 只是一个 java 网络应用程序...当它收到服务请求时,它会查找您为其定义的映射。如果找到映射,它会尝试创建您定义的必要类的实例来为请求提供服务。

如果您将类定义为抽象类或接口(interface)类,那么您将得到 InstantiationExceptions,因为无法创建这些类型。当 Axis 尝试创建 wsdl 时,它无法确定要放置的类型,因此它将使用“anyType”。

回答您的问题:您可以在代码中使用上面的模型,但不能将这些类与 Axis 一起使用。我们在项目中通常做的是:

  1. 定义我们需要的类,就像我们在典型的面向对象应用程序中所做的那样
  2. 定义用于网络服务的“仅传输”类。这些类由简单的类型组成,可以很容易地创建。它们仅用于交换网络服务消息。我们将这些类与 Axis 一起使用。
  3. 为这两类人找到一些方法来轻松地共享/交换信息。您可以拥有由两者共享的接口(interface)(但 Axis 不知道),甚至可以使用 BeanUtils.copyProperites 使两个不同的对象保持同步。

希望这能回答您的问题。

关于wsdl - Axis wsdl 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/546779/

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