我正在使用Eclipse,Axis1和JBoss开发一个简单的Web服务。 Web服务类如下所示:
public class MyService {
public ComplexClass getSomeData() {
ComplexClass complex = new ComplexClass();
Children children[] = new Children[2];
children[0] = new Children();
complex.myFirstArray = children;
MoreChildren[] moreChildren = new MoreChildren[2];
moreChildren[0] = new MoreChildren();
children[0].mySecondArray = moreChildren;
return complex;
}
}
要创建Web服务,我右键单击MyService类,选择Create Web Service,使用Axis1,document / literal(wrapped)。
ComplexClass,Children和MoreChildren的定义:
public class ComplexClass {
public String name;
public int age;
public Children[] myFirstArray;
public ComplexClass() {
this.name = "defaultName";
this.age = -1;
this.myFirstArray = new Children[0];
}
}
public class Children {
public int attribute;
public MoreChildren[] mySecondArray;
public Children() {
this.attribute = 1;
this.mySecondArray = new MoreChildren[0];
}
}
public class MoreChildren {
public String someValue;
public MoreChildren() {
this.someValue = "defaultValue";
}
}
生成的wsdl如下所示:
<wsdl:definitions targetNamespace="http://example" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://example" xmlns:intf="http://example" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://example" xmlns="http://www.w3.org/2001/XMLSchema">
<element name="getSomeData">
<complexType/>
</element>
<element name="getSomeDataResponse">
<complexType>
<sequence>
<element name="getSomeDataReturn" type="impl:ComplexClass"/>
</sequence>
</complexType>
</element>
<complexType name="MoreChildren">
<sequence>
<element name="someValue" nillable="true" type="xsd:string"/>
</sequence>
</complexType>
<complexType name="ArrayOfMoreChildren">
<sequence>
<element maxOccurs="unbounded" minOccurs="0" name="item" type="impl:MoreChildren"/>
</sequence>
</complexType>
<complexType name="Children">
<sequence>
<element name="attribute" type="xsd:int"/>
<element name="mySecondArray" nillable="true" type="impl:ArrayOfMoreChildren"/>
</sequence>
</complexType>
<complexType name="ArrayOfChildren">
<sequence>
<element maxOccurs="unbounded" minOccurs="0" name="item" type="impl:Children"/>
</sequence>
</complexType>
<complexType name="ComplexClass">
<sequence>
<element name="name" nillable="true" type="xsd:string"/>
<element name="age" type="xsd:int"/>
<element name="myFirstArray" nillable="true" type="impl:ArrayOfChildren"/>
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="getSomeDataResponse">
<wsdl:part element="impl:getSomeDataResponse" name="parameters"/>
</wsdl:message>
<wsdl:message name="getSomeDataRequest">
<wsdl:part element="impl:getSomeData" name="parameters"/>
</wsdl:message>
<wsdl:portType name="MyService">
<wsdl:operation name="getSomeData">
<wsdl:input message="impl:getSomeDataRequest" name="getSomeDataRequest"/>
<wsdl:output message="impl:getSomeDataResponse" name="getSomeDataResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="MyServiceSoapBinding" type="impl:MyService">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getSomeData">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getSomeDataRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getSomeDataResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MyServiceService">
<wsdl:port binding="impl:MyServiceSoapBinding" name="MyService">
<wsdlsoap:address location="http://localhost:8080/NutriAdvisorWS/services/MyService"/>
</wsdl:port>
</wsdl:service>
<style/>
</wsdl:definitions>
问题是,在为这个Web服务生成客户端之后,使用基于axis1的Eclipse向导,我使用这样的客户端代码:
public static void main(String[] args) throws RemoteException {
MyServiceProxy s = new MyServiceProxy();
ComplexClass c = s.getSomeData();
System.out.println("I got: "+ c);
}
我得到以下例外:
org.xml.sax.SAXException: Invalid element in example.Children - someValue
at org.apache.axis.encoding.ser.BeanDeserializer.onStartChild(BeanDeserializer.java:258)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at example.MyServiceSoapBindingStub.getSomeData(MyServiceSoapBindingStub.java:187)
at example.MyServiceProxy.getSomeData(MyServiceProxy.java:50)
at TestMyService.main(TestMyService.java:15)
在做了一些测试之后,我缩小了问题并注意到在ComplexType返回的对象中使用嵌套数组时会发生这种情况。知道怎么解决这个问题吗?
调用Web服务时得到的输出是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getSomeDataResponse xmlns="http://example">
<getSomeDataReturn>
<name>defaultName</name>
<age>-1</age>
<myFirstArray>
<attribute>1</attribute>
<mySecondArray>
<someValue>defaultValue</someValue>
</mySecondArray>
<mySecondArray xsi:nil="true"/>
</myFirstArray>
<myFirstArray xsi:nil="true"/>
</getSomeDataReturn>
</getSomeDataResponse>
</soapenv:Body>
</soapenv:Envelope>