調用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
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...