从 Spyne 响应变量中删除命名空间

c4t*_*yst 6 python soap wsdl spyne

根据特定的 WSDL 实现 WebService。客户端无法更改。正确处理来自客户端的请求,但客户端因变量中的命名空间而抱怨响应。

我想要什么(基于 WSDL 的 SoapUI 响应):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cal="http://callback.foo.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <cal:foo_statusResponse>
         <result>SUCCESS</result>
         <notify>Thanks!</notify>
      </cal:foo_statusResponse>
   </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

我得到的结果(tns:有关导致验证问题的变量的通知):

<senv:Envelope xmlns:tns="http://callback.foo.com/" xmlns:senv="http://schemas.xmlsoap.org/soap/envelope/">
  <senv:Body>
    <tns:foo_statusResponse>
      <tns:result>SUCCESS</tns:result>
      <tns:notify>Thanks!</tns:notify>
    </tns:foo_statusResponse>
  </senv:Body>
</senv:Envelope>
Run Code Online (Sandbox Code Playgroud)

Java 客户端抛出此异常:

[com.sun.istack.SAXParseException2; 行号:2;列数:162;意外元素(uri:“ http://callback.foo.com/ ”,本地:“结果”)。预期元素为 <{}result>,<{}notify>]

实现片段:

class fooStatusRS(ComplexModel):
    result = Unicode()
    notify = Unicode()

class foo_callback(ServiceBase):
    @srpc(Unicode, Unicode, Unicode, Unicode, statusbarInfo, anotherResponse, 
            _out_header=None, 
            _out_variable_names=("result", "notify"), 
            _returns=(Unicode, Unicode), 
            _out_message_name="foo_statusResponse",
            _operation_name="foo_status_rq")
    def foo_status(foo_id, reply, ref, status, statusbar, another):
        if foo_id:
            print foo_id

        return fooStatusRS(result="SUCCESS", notify="Foo received!")
Run Code Online (Sandbox Code Playgroud)

c4t*_*yst 5

这是不可能的(目前), 维护者在类似的问题中回答了这一问题。

解决方法是向 event_manager 添加“method_return_string”的侦听器,然后执行一些字符串操作。

def _method_return_string(ctx):
    ctx.out_string[0] = ctx.out_string[0].replace("tns:result>", "result>")
    ctx.out_string[0] = ctx.out_string[0].replace("tns:notify>", "notify>")
Run Code Online (Sandbox Code Playgroud)