views:

814

answers:

1

Hi, I'm using Axis to model a sample WebService. What I'm doing now is trying to understand which are the limitations of the automated wsdl and code generation.

Now for some server side code:

this is the skeleton of the sample web service:

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

and my data classes: 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 */
}

The eclipse tool for Axis2 is quite happy to generate a wsdl from these sources, including the schema for the attribute classes, which is:

<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>

now, if see something strange here, AbstractAttribute hasn't the ** abstract="true" ** attribute, and define an anyType value element, which get rewrite in IntAttribute and StirngAttribute. I don't even know if this is a legal schema (I don't think it's legal, by the way).

More, if I try to generate a client from this wsdl (always using the eclipse tools) the generated sources won't compile, because AbstractAttribute defines an

Object localValue;

field and Int/String Attribute defines

int localValue;

and

String localValue;

..I tried to "accomodate" the sources (without many hopes, obviously), and the results are that the server try to instantiate an AbstractAttribute instance (throwing an InstantiationException).

So my question is: there is a way to model something like the data model above, or web services and XML schemas' in general are not the best tools to use for this particular case?

+2  A: 

To explain the problem you are encountering, it helps to think of what Axis needs to do when your service is called.

Axis is simply a java web-application...when it receives a request for a service, it will look up the mapping that you've defined for it. If it finds a mapping, it tries to create an instance of the necessary classes you've defined to service the request.

If you've defined the classes as abstract or as interfaces then you'll get InstantiationExceptions since these types can't be created. When Axis tried to create the wsdl, it won't be able to figure out what type to put so it will use "anyType."

To answer your question: you CAN use the the model you have above in your code, but you won't be able to use these classes with Axis. What we have typically done in our projects is:

  1. Define the classes we need, as we would in a typical Object-Oriented application
  2. Define "transport-only" classes that are used for web services. These classes are composed of simple types and can be easily created. They are only used for exchanging web-service messages. We use these classes with Axis.
  3. Find some way for these two types of classes to easily share/exchange information. You can have interfaces that are shared by both (but Axis doesn't know about) or even use BeanUtils.copyProperites to keep two different objects in sync.

Hope that answers your question.

Michael Sharek