Java 返回一个带有嵌套数组的complexType类来破坏web服务,为什么?
我正在使用Eclipse、Axis1和JBoss开发一个简单的web服务。web服务类如下所示:Java 返回一个带有嵌套数组的complexType类来破坏web服务,为什么?,java,web-services,soap,wsdl,axis,Java,Web Services,Soap,Wsdl,Axis,我正在使用Eclipse、Axis1和JBoss开发一个简单的web服务。web服务类如下所示: public class MyService { public ComplexClass getSomeData() { ComplexClass complex = new ComplexClass(); Children children[] = new Children[2]; children[0] = new Children();
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;
}
}
<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>
public static void main(String[] args) throws RemoteException {
MyServiceProxy s = new MyServiceProxy();
ComplexClass c = s.getSomeData();
System.out.println("I got: "+ c);
}
要创建web服务,我右键单击MyService类,选择Createwebservice,usingAxis1,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如下所示:
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;
}
}
<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>
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>
有人能解释一下anidated的意思吗?对不起,我指的是嵌套数组@斯蒂芬斯,你能克服这一切吗?我面临着完全相同的问题,找不到任何解决办法。我使用的是Axis1.6.1