【问题标题】:How to call a SOAP Web Service using Ruby Savon?如何使用 Ruby Savon 调用 SOAP Web 服务?
【发布时间】:2013-08-01 23:37:41
【问题描述】:

我正在尝试使用 Savon 调用 Web 服务。 我要生成的请求是这样的(这是一个有效的请求,使用wizdler 生成):

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <FraudValidationRequest xmlns="http://schemas.gid.gap.com/fraudvalidation/v3">
            <OrderHeader xmlns="">
                <EntryType>1</EntryType>
.... more attributes

但是我得到了这样的东西:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wsdl="http://schemas.gid.gap.com/fraudvalidation/v3"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <wsdl:validateOrder>
      <fraudValidationRequest>
        <orderHeader>
          <entryType>1</entryType>
        </orderHeader>
      </fraudValidationRequest>
    </wsdl:validateOrder>
  </env:Body>
</env:Envelope>

所以我在服务器端得到这个错误(web服务是用java实现的):

org.apache.axis2.databinding.ADBException: Unexpected subelement FraudValidationRequest

这是我的 ruby​​ 客户端代码:

require "savon"
URL = 'http://localhost:8080/MockFraudValidationServiceProvider/services/FraudValidationServiceV3'
begin
    client = Savon.client do
        # wsdl URL + "?wsdl"
        endpoint URL
        namespace "http://schemas.gid.gap.com/fraudvalidation/v3"
        log_level :debug
        pretty_print_xml :true
    end
    response = client.call(:validate_order,
        message: {
            FraudValidationRequest: { OrderHeader: { EntryType: 1 } } 
        }
    )
    puts response.to_hash;
end

我尝试了几件事:wsdl、端点和命名空间、有/没有命名空间、驼峰式与否等,但我无法生成适当的请求。 我不是 SOAP 专家(显然),我知道如果有 WSDL(我的情况),则无需设置命名空间,但我不确定。当我尝试仅使用 WSDL 时,我得到了这个:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:types="http://services.gid.gap.com/fraudvalidation/v3"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ins0="http://schemas.gid.gap.com/fraudvalidation/v3">
  <env:Body>
    <types:FraudValidationRequest>
      <fraudValidationRequest>
        <orderHeader>
          <entryType>1</entryType>
        </orderHeader>
      </fraudValidationRequest>
    </types:FraudValidationRequest>
  </env:Body>
</env:Envelope>

请指教,希望我清楚。

【问题讨论】:

  • 您是否尝试过使用 SoapUI 手动创建 SOAP 调用?那将是起点。然后,您应该将正确的 SOAP 调用的 XML 文档共享到您的界面。
  • 在我的问题的第一部分,我分享了一个由wizdler 生成的正确调用。

标签: ruby web-services savon


【解决方案1】:

你试过了吗?

require 'savon'

# create a client for the service
client = Savon.client(wsdl: 'http://service.example.com?wsdl')

p client.operations
# => [:find_user, :list_users]

# call the 'findUser' operation
response = client.call(:find_user, message: { id: 42 })

response.body
# => { find_user_response: { id: 42, name: 'Hoff' } }

客户端操作是您可以调用的操作,您只需打印它们并检查以调用正确的操作。 message 是一个客户端参数,如果你放了你的参数,你也可以这样设置:

params = {
  :param_1 => "value",
  _param_2 => 7
}
response = client.call(:find_user, message: params)

我使用此代码并且能够调用我的 Web 服务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2016-05-27
    • 2013-12-05
    相关资源
    最近更新 更多