我對SOAP/web服務比較陌生;雖然我已經完成了一些較小的Web服務項目,但我從來不需要返回(或用作參數)一個數組或一組「複雜」對象的集合。當我嘗試這樣做時,根據我的SOAP綁定樣式,我會得到不同的奇怪行爲。當我使用RPC/literal,我可以發送和接收內置類型數組(例如String []),但是當我嘗試一個'複雜'對象時,我得到編組錯誤(例如xyz或任何其他錯誤它的超級類別在本文中是已知的),儘管已將相關類添加到@XmlSeeAlso註釋中。如何使用Java中的Web服務(例如Axis2)發送數組或複合對象的集合?
或者,試圖文檔/文字/包裹(這似乎是最好的做法是什麼?)讓我來發送或接收復雜的對象,但會導致一些古怪,當我試圖繞過任何類型的數組。例如,String[] t = { "A", "B", "C", "D" };
作爲空的字符串(不是字符串[])到達Web服務。
有沒有人有任何想法這裏發生了什麼事情或如何讓事情以任何明智的方式工作?我現在在本地部署Apache Geronimo。
服務器的代碼片段:
接口:
@WebService
@SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL, parameterStyle=ParameterStyle.WRAPPED)
@XmlSeeAlso({Feedback.class,Feedback[].class})
public interface CerberusRequest {
...
@WebMethod
public Feedback[] test(String[] facts) throws Exception;
}
實現:
@WebService(serviceName = "CerberusRequestService", portName
= "CerberusRequestPort", targetNamespace = "http://web.cerberus.xyz.gov/",
endpointInterface = "gov.xyz.cerberus.web.CerberusRequest")
public class CerberusRequestImpl implements CerberusRequest {
...
public Feedback[] test(String[] facts) throws Exception {
//Just some debug crap (prints: "in test...1 [Ljava.lang.String;")
System.out.println("in test..." + facts.length + " " + facts.getClass().getName());
for(String f : facts) {
System.out.println("test:" + f);
}
//return feedback;
Feedback[] f = { new Feedback() };
return f;
}
}
客戶端的代碼片段:
URL url = new URL(destination+"?wsdl");
QName qname = new QName(namespace, service);
Service service = Service.create(url, qname);
CerberusRequest sr = service.getPort(CerberusRequest.class);
String[] t = { "A", "B", "C", "D" };
Feedback[] feedback = sr.test(t);
for(Feedback f : feedback) {
System.out.println(f);
}
WSDL:
<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://web.cerberus.xyz.gov/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CerberusRequestService" targetNamespace="http://web.cerberus.xyz.gov/">
<types>
<xsd:schema>
<xsd:import namespace="http://web.cerberus.xyz.gov/" schemaLocation="http://localhost:8080/CerberusService/CerberusRequest?xsd=xsd1"/>
</xsd:schema>
<xsd:schema>
<xsd:import namespace="http://jaxb.dev.java.net/array" schemaLocation="http://localhost:8080/CerberusService/CerberusRequest?xsd=xsd2"/>
</xsd:schema>
</types>
<message name="Exception">
<part element="tns:Exception" name="fault">
</part>
</message>
<message name="test">
<part element="tns:test" name="parameters">
</part>
</message>
<message name="testResponse">
<part element="tns:testResponse" name="parameters">
</part>
</message>
<portType name="CerberusRequest">
<operation name="test">
<input message="tns:test">
</input>
<output message="tns:testResponse">
</output>
<fault message="tns:Exception" name="Exception">
</fault>
</operation>
</portType>
<binding name="CerberusRequestPortBinding" type="tns:CerberusRequest">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="test">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
<fault name="Exception">
<soap:fault name="Exception" use="literal"/>
</fault>
</operation>
</binding>
<service name="CerberusRequestService">
<port binding="tns:CerberusRequestPortBinding" name="CerberusRequestPort">
<soap:address location="http://localhost:8080/CerberusService/CerberusRequest"/>
</port>
</service>
</definitions>
非常感謝您事先的任何和所有幫助您可以提供!