簡體   English   中英

使用SUDS時添加xsi:type和信封命名空間

[英]Adding xsi:type and envelope namespace when using SUDS

我需要與SOAP服務進行交互,但是這樣做很麻煩; 非常感謝您對此提出任何建議。 原始錯誤消息是:

org.apache.axis2.databinding.ADBException: Any type element type has not been given

經過研究,結果表明這是SUDS之間的分歧,服務器必須如何處理

type="xsd:anyType"

在有問題的元素上。

我已確認使用SOAPUI,並在提出建議后可以采取以下步驟解決此問題:

  1. 將xsi:type =“ xsd:string”添加到每個會導致問題的元素
  2. 將xmlns:xsd =“ http://www.w3.org/2001/XMLSchema”添加到SOAP信封

因此,SUDS當前在哪里執行此操作:

<SOAP-ENV:Envelope ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<ns3:Body>
  <ns0:method>
     <parameter>
        <values>
           <table>
              <key>EMAIL_ADDRESS</key>
              <value>example@example.org</value>
           </table>
        </values>
     </parameter>
  </ns0:method>

它應該改為產生以下內容:

<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" ... xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

  <ns3:Body>
  <ns0:method>
     ...
     <parameter>
        <values>
           <table>
              <key xsi:type="xsd:string">EMAIL_ADDRESS</key>
              <value xsi:type="xsd:string">example@example.org</value>
           </table>
        </values>
     </parameter>
  </ns0:method>

有正確的方法嗎? 我已經看到了使用ImportDoctor或MessagePlugins的建議,但是還沒有真正了解如何實現所需的效果。

我發現的解決方案是使用MessagePlugin在發送前實質上手動修復XML。 我希望有一些更優雅的東西,但是至少可以起作用:

class SoapFixer(MessagePlugin):

    def marshalled(self, context):
        # Alter the envelope so that the xsd namespace is allowed
        context.envelope.nsprefixes['xsd'] = 'http://www.w3.org/2001/XMLSchema'
        # Go through every node in the document and apply the fix function to patch up incompatible XML. 
        context.envelope.walk(self.fix_any_type_string)

    def fix_any_type_string(self, element):
        """Used as a filter function with walk in order to fix errors.
        If the element has a certain name, give it a xsi:type=xsd:string. Note that the nsprefix xsd must also
         be added in to make this work."""
        # Fix elements which have these names
        fix_names = ['elementnametofix', 'anotherelementname']
        if element.name in fix_names:
            element.attributes.append(Attribute('xsi:type', 'xsd:string'))

就像這個特定庫中的許多內容一樣,這令人傷心和搞笑,但這是確切的答案:

http://lists.fedoraproject.org/pipermail/suds/2011-September/001519.html

從上面:

soapenv = soapenv.encode('utf-8')
plugins.message.sending(envelope=soapenv)

變成:

soapenv = soapenv.encode('utf-8')
ctx = plugins.message.sending(envelope=soapenv)
soapenv = ctx.envelope

基本上,這是實現過程中的錯誤,您可以通過編輯運行插件的行來自己對其進行修補,以實際返回插件的結果,但是我還不知道已修復此問題的SUDS的修補和更新版本(我沒有仔細尋找它)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM