調用java的webservice返回null

来源:http://www.cnblogs.com/itpro/archive/2016/03/25/5320170.html
-Advertisement-
Play Games

When you try invoke a Java/Axis Web Service from a proxy class generated by Visual Studio 2005 or Visual Studio 2008 you often crash against the ‘retu ...


When you try invoke a Java/Axis Web Service from a proxy class generated by Visual Studio 2005 or Visual Studio 2008 you often crash against the ‘return null’ issue.

The web service seems to get called correctly and it responds to your client in the right way (you have no exception of any sort), but your returned object is null, it happened to me to face this situation today for the first time, there are a couple of things you can do to debug and resolve this situation:

  • Let’s consider our function call:
       1: [Test]
       2: public void T1()
       3: {
       4:     Test.TestWs ws = new AxisWebService.Test.TestWs ();
       5:     Test.State[] arr = ws.getStates(1);
       6:     Assert.IsNotNull(arr);
       7: }

here we expect to have back an array of State objects, instead we obtain the hated ‘null’.

  • The first thing to do is to download and install ‘Fiddler’ (do it right now if you don’t have it already) and have a look at what the web service respond to us (the snippet is a trimmed response):
       1: HTTP/1.1 200 OK
       2: Connection: close
       3: Date: Wed, 15 Oct 2008 12:36:26 GMT
       4: Server: Microsoft-IIS/6.0
       5: X-Powered-By: ASP.NET
       6: Content-Type: text/xml;charset=utf-8
       7: 
       8: <?xml version="1.0" encoding="utf-8"?>
       9: <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">
      10: <soapenv:Body>
      11: <getStatesResponse xmlns="">
      12: <item xsi:type="ns1:State" xmlns:ns1="http://mynamespace.it/">
      13: <Code>A001</Code>
      14: <Description>Test</Description>
      15: </item>
      16: </getStatesResponse>
      17: </soapenv:Body>
      18: </soapenv:Envelope>

Since the web service is responding correctly the problem is in the deserialization stage of the data stream sent back by the web service.

  • It’s time to show some hidden file of the solution and look inside the ‘reference.cs’ (this is the default file in which Visual Studio creates some proxy classes).

Looking at the proxy classes generated by Visual Studio, it seems that we have all that we need: a class to call the web service and series of classes that map the objects the service returns; where’s the problem then? it turns out that the web service client can’t understand the response stream, so the problem is in a mismatch somewhere.

Given the fact we have all the classes and all of them have the right properties, it’s time to look for the namespaces:

  • Visual Studio 2005
       1: [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://mynamespace.it/getStates",
       2:     Use = System.Web.Services.Description.SoapBindingUse.Literal,
       3:     ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)]
       4: [return: System.Xml.Serialization.XmlArrayAttribute("getStatesResponse", Namespace = "http://mynamespace.it/")]
       5: [return: System.Xml.Serialization.XmlArrayItemAttribute("item", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
       6: public Stato[] getStates([System.Xml.Serialization.XmlElementAttribute(Namespace = "http://mynamespace.it/")] int getStatesRequest)
       7: {
       8:     object[] results = this.Invoke("getStates", new object[] {
       9:                 getStatesRequest});
      10:     return ((State[])(results[0]));
      11: }

pay attention to line number 4:

   1: [return: System.Xml.Serialization.XmlArrayAttribute("getStatesResponse", Namespace = "http://mynamespace.it/")]

here it states that the getResponseState element is qualified with the ‘http://mynamespace.it'/’ namespace...but look at what Fiddler captured for us (line 11 of the previous snippet): there we see that the namespace associated with the element is “” (empty string), so here it is our mismatch. To fix the problem you have to manually edit the attribute and correct the namespace to “” (empty string).

Be very careful: writing Namespace = “” or removing it at all are two completely different things.

Having made this fix our test passes and we are able to get our objects back from the web service.

  • Visual Studio 2008 
    It produces a completely different set of classes to call the web service, we have an interface that describes the service, a series of classes that represent the request and response of each method exposed by the interface and finally we have the proxy classes for the objects returned. We know that the problem is at the ‘client’ side so checking the request classes is useless, we focus our attention on the response classes and on object classes to verify the namespace mappings:
       1: [System.Diagnostics.DebuggerStepThroughAttribute()]
       2: [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
       3: [System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
       4: public partial class getStatesResponse {
       5:    
       6:     [System.ServiceModel.MessageBodyMemberAttribute(Name="getStatesResponse", Namespace="http://mynamespace.it/", Order=0)]
       7:     [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
       8:     public State[] getStatesResponse1;
       9:    
      10:     public getStatesResponse() {
      11:     }
      12:    
      13:     public getStatesResponse(State[] getStatesResponse1) {
      14:         this.getStatesResponse1 = getStatesResponse1;
      15:     }
      16: }

look at line 6, you can see a namespace mismatch again, the fix is the same applied before.

In the end, if you are using a Java/Axis Web Service and you get null results from you service calls, don't trust the auto-generated proxy classes too much and check that the attribute that defines the namespace for each object match what you get from the wsdl and from the traced response.

 

引用地址:<http://www.primordialcode.com/blog/post/invoking-javaaxis-web-service-net-return-null-issue


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • BOSS: Bidrectional Operating Sytem Scheduler (uITRON and Linux) 雙向的操作系統調度 控制ARM異常處理會將異常相應的派遣到uITRON或者Linux端 除了linux的ISR, 整個linux 系統作為uITRON 的來賓任務被執行。
  • 安裝過程 wget http://www.asty.org/cmatrix/dist/cmatrix-1.2a.tar.gztar xvf cmatrix-1.2a.tar.gzcd cmatrix-1.2ayum install ncurses-devel./configure && make &
  • 所有目錄務必保持具有X許可權!!,否則無法進入該目錄及子目錄,且無法讀取該目錄及子目錄下的文件或子目錄
  • 最近在Linux平臺上配置伺服器部署網站(說多了都是淚!),記個筆記! 一、首先是在centos下安裝mysql (參考博客) mysql yum庫提供了一個簡單的和方便的方法來安裝和更新MySQL相關的軟體包到最新版本。 參考文檔:http://dev.mysql.com/downloads/re
  • 本文將tomcat安裝到了/alidata/server/目錄下,當然也可以安裝到其他目錄。 1. 下載tomcat:#wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.54/bin/apache-tomcat-7.0.54.tar.gz 2. 將t
  • 2016-03-18 17:10:19 張超《Linux內核分析》MOOC課程http://mooc.study.163.com/course/USTC-1000029000 我的實驗平臺以及代碼見https://www.shiyanlou.com/courses/reports/986221 實驗
  • 如何查看、備份電腦隱藏的恢復分區 步驟: 1.用管理員身份打開CMD(快捷鍵:1. 同時按 win+x 2. 按 a ),輸入diskpart,回車enter。 (或者 按 win+r 在運行中輸入 diskpart 也行)。 2.輸入 lis dis ,回車。 (lis dis 是 list di
  • 1、安裝全部桌面環境,其實Ubuntu系列桌面實際上有幾種桌面應用程式,包括Ubuntu-desktop、Kubunut-desktop和Xubuntu- desktop。 我們就安裝了Ubuntu-desktop還有Gnome因為安裝桌面相關軟體太多。 命令:#sudo aptitude inst
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...