3

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

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

这是示例 Web 服务的框架:

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

和我的数据类: public abstract class AbstractAttribute { String name;

  /*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 模式通常不是用于这种特殊情况的最佳工具?

4

1 回答 1

4

要解释您遇到的问题,请考虑在调用您的服务时 Axis 需要做什么。

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

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

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

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

希望这能回答你的问题。

于 2009-02-16T20:21:38.780 回答