返回带有嵌套数组的复杂类型类会破坏 Web 服务,为什么?
我正在使用 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 类,选择“创建 Web 服务”,使用 Axis1、文档/文字(包装)。
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 服务生成客户端之后,使用 Eclipse 向导,基于 axis1,我使用如下客户端代码:
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 返回的对象中使用嵌套数组时会发生这种行为。知道如何解决这个问题吗?
调用网络服务时得到的输出是:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论