9

我正在使用savon 版本 2(使用 Ruby on Rails)来调用 Web 服务,并且我需要向我的 Envelope 注入一些额外的命名空间。就像是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:add="http://schemas.xmlsoap.org/ws/2003/03/addressing" 
xmlns:newNamespace1="http://someURL.pt/Test1" 
xmlns:newNamespace2="http://someURL.pt/Test2" 
xmlns:newNamespace3="http://someURL.pt/Test3"

我目前的代码是:

client = Savon.client do
        wsdl "https://someValidURL?wsdl"

        namespace "http://someURL.pt/Test1" 
        namespace "http://someURL.pt/Test2" 
        namespace "http://someURL.pt/Test3"
end

response = client.call( ...the webservice call... )

...但在我的要求中,Savon 只放置了最后一个命名空间

<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsns="http://someURL.pt/Test3" 
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

我在Savon Git project上没有找到任何关于此的文档。

有没有人有解决这个问题的方法??

PS-我还检查了一种可能的解决方案是将所有 xml 请求(信封)设置为请求,但是......嗯......太像黑客了。

如果这是不可能的并且有其他好的宝石可以做到这一点,请告诉我=)

4

2 回答 2

10

我发现(目前)不可能在 Savon 版本 2 上设置多个命名空间。

现在我在 Savon 版本 1 上迁移我的应用程序并且它有效 =)

begin
    client = Savon::Client.new do
        wsdl.document = "https://someURL?wsdl"
    end

  @response = client.request :service do
       soap.namespaces["xmlns:test1"]  = "http:/someURLtest1"
       soap.namespaces["xmlns:test2"]  = "http:/someURLtest2" 

       soap.body = { #... the message....
          :"test1:something" => {}, 
          :"test2:something1" => {}
                   }
   end


  rescue Savon::Error => error
     log error.to_s
  end

更多信息在这里这里

这个问题将在 Savon 2 的下一个版本中使用以下代码解决:

namespaces(
   "xmlns:first" => "http://someURL.pt/Test1",
   "xmlns:two"   => "http://someURL.pt/Testweewqwqeewq"
)
于 2013-01-25T16:11:21.110 回答
8

Savon 2.1.0开始,可以通过namespaces使用命名空间定义的散列设置键来完成:

Savon.client({
  ...
  namespaces: {
    'xmlns:first' => 'http://someURL.pt/Test1',
    'xmlns:second' => 'http://someURL.pt/Test2',
    'xmlns:nth' => 'http://someURL.pt/TestN'
  }
})
于 2016-06-08T18:09:36.057 回答