2010-07-01 83 views
5

任何一個身影從我的問題可能是...Java的web服務返回null對象到.NET客戶端

我打電話從.NET客戶端一個Java web服務的一個WebMethod(軸1.4)。該方法返回一個Map對象,如果我從Axis客戶端調用它,可以正常工作,但在我的C#代碼中,它始終爲空。

這就是WSDL:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="urn:http.service.enlaces.portlet.ext.com" xmlns:intf="urn:http.service.enlaces.portlet.ext.com" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://model.enlaces.portlet.ext.com" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:http.service.enlaces.portlet.ext.com"> 

<wsdl:types> 

<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xml.apache.org/xml-soap"> 
<import namespace="urn:http.service.enlaces.portlet.ext.com"/> 
<import namespace="http://model.enlaces.portlet.ext.com"/> 
<import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> 
<complexType name="mapItem"> 
    <sequence> 
<element name="key" nillable="true" type="xsd:anyType"/> 
<element name="value" nillable="true" type="xsd:anyType"/> 
    </sequence> 
</complexType> 
<complexType name="Map"> 
    <sequence> 
    <element maxOccurs="unbounded" minOccurs="0" name="item" type="apachesoap:mapItem"/> 
    </sequence> 
</complexType> 
</schema> 
</wsdl:types> 

<wsdl:message name="getFoldersAndBookmarksRequest" /> 
<wsdl:message name="getFoldersAndBookmarksResponse"> 
    <wsdl:part name="getFoldersAndBookmarksReturn" type="apachesoap:Map" /> 
</wsdl:message> 

<wsdl:portType name="BookmarksEntryServiceSoap"> 
<wsdl:operation name="getFoldersAndBookmarks"> 
     <wsdl:input name="getFoldersAndBookmarksRequest" message="intf:getFoldersAndBookmarksRequest" /> 
     <wsdl:output name="getFoldersAndBookmarksResponse" message="intf:getFoldersAndBookmarksResponse" /> 
    </wsdl:operation> 
    </wsdl:portType> 

<wsdl:binding name="Portlet_Bookmarks_BookmarksEntryServiceSoapBinding" type="intf:BookmarksEntryServiceSoap"> 
    <wsdlsoap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> 
    <wsdl:operation name="getFoldersAndBookmarks"> 
     <wsdlsoap:operation soapAction="" /> 
     <wsdl:input name="getFoldersAndBookmarksRequest"> 
     <wsdlsoap:body use="encoded" namespace="urn:http.service.enlaces.portlet.ext.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
     </wsdl:input> 
     <wsdl:output name="getFoldersAndBookmarksResponse"> 
     <wsdlsoap:body use="encoded" namespace="urn:http.service.enlaces.portlet.ext.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" /> 
     </wsdl:output> 
    </wsdl:operation> 
</wsdl:binding> 

和我的C#自動生成的代碼:

[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="urn:http.service.enlaces.portlet.ext.com", ResponseNamespace="urn:http.service.enlaces.portlet.ext.com")] 
[return: System.Xml.Serialization.SoapElementAttribute("getFoldersAndBookmarksReturn")] 
public Map getFoldersAndBookmarks() { 
    object[] results = this.Invoke("getFoldersAndBookmarks", new object[0]); 
    return ((Map)(results[0])); 
} 

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.SoapTypeAttribute(Namespace="http://xml.apache.org/xml-soap")] 
public partial class Map { 

    private mapItem[] itemField; 

    /// <comentarios/> 
    public mapItem[] item { 
     get { 
      return this.itemField; 
     } 
     set { 
      this.itemField = value; 
     } 
    } 
} 

我,已經隨處可見不幸的是,我沒有找到解決辦法。 請問有人知道嗎?

+0

我們碰到了類似的問題,在被軸產生的SOAP服務提供.NET不能正確反序列化數組。不幸的是,對我們來說,解決方案是使用Axis之外的其他產品來生成Web服務。我工作在.NET的.NET領域,所以我不能確切地說它是什麼(或者我會發佈一個答案);但是,考慮查看其他基於Java的SOAP服務引擎。 – kbrimington 2010-08-17 03:44:59

回答

0

這就是爲什麼從代碼生成的Web服務是幾乎從來沒有互通:)

一個解決此工作的好辦法是使WSDL第一,並定義XSD的一個很好的明確一點,那應該映射很好地融入.Net和Java。如果你有任何控制權,服務器的另一種選擇是服務器的軸1.4以外的東西(痛苦)。

最後,嘗試按摩java代碼中的簽名,嘗試用MapItem []替換List,反之亦然,請確保您沒有Map在返回對象或參數中的任何位置。

再次檢查您生成的wsdl,我會說這可能是因爲mapItem的鍵/值部分的xsd:anyType。

我認爲這是軸生成的,如果你在參數中有一個java對象。相信我,你不想那樣。使它成爲一個字符串,或一個複雜類型或一個整數,但一個對象只能意味着開放式XML(xsd:anyType),而不是許多客戶端沒有如何解析它。

0

我前段時間遇到過同樣的問題。當您嘗試通過使用.net客戶端的軸webservice獲取元素數組時,會發生這種情況。

問題是這一行的「名=項目」部分:

<element maxOccurs="unbounded" minOccurs="0" name="item" type="apachesoap:mapItem"/> 

嘗試在特定行「項目」到「mapItem」轉變。嘗試下列操作之一:

<element maxOccurs="unbounded" minOccurs="0" name="mapItem" type="apachesoap:mapItem"/> 

<element maxOccurs="unbounded" minOccurs="0" name="key" type="apachesoap:mapItem"/> 

<element maxOccurs="unbounded" minOccurs="0" name="value" type="apachesoap:mapItem"/> 
1

所以很晚幫助你,但我最近遇到了同樣的問題。

首先,我使用Eclipse創建一個Web服務。對我來說,問題在於生成的wsdd使用的是「document/literal(wrapped)」風格。將其更改爲「RPC」解決了該問題。沒有更多的空值。

所以,也許如果你改變你的編碼到RPC可能會解決你的問題。

0

我面對的是,我不得不改變WSDL文件,以便:

<wsdlsoap:body use="encoded" ... 

<wsdlsoap:body use="literal" ... 

只有執行代理生成。

0

我面臨同樣的問題。我的解決方案是在自動生成的函數中刪除名稱空間。 這是我的函數:

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.service-now.com/incident/getRecords", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)] 
[return: System.Xml.Serialization.XmlArrayAttribute("getRecordsResponse", Namespace = "")] 
[return: System.Xml.Serialization.XmlArrayItemAttribute("getRecordsResult", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)] 
public getRecordsResponseGetRecordsResult[] getRecords([System.Xml.Serialization.XmlElementAttribute("getRecords", Namespace = "http://www.service-now.com/incident")] getRecords getRecords1) 
{ 
    object[] results = this.Invoke("getRecords", new object[] { 
       getRecords1}); 
    return ((getRecordsResponseGetRecordsResult[])(results[0])); 
} 

我刪除了命名空間在這條線。當我通過SoapUI測試Web服務時,我意識到響應對象沒有名稱空間。但自動生成的代碼具有名稱空間。

[return: System.Xml.Serialization.XmlArrayAttribute("getRecordsResponse", Namespace = "")] 

了SoapUI響應是如下:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <SOAP-ENV:Body> 
     <getRecordsResponse> 
     <getRecordsResult> 
      <active>0</active> 
     </getRecordsResult> 
     </getRecordsResponse> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope>