Ruby Savon 2 gem:无法指定XML子元素和子子元素来向SOAP API Webservice发出有效请求

时间:2015-06-18 08:57:02

标签: ruby xml soap soapui savon

做了大量的谷歌搜索并查看了Savon 2文档,但我认为我没有在我的ruby代码中正确指定我需要对此SOAP webservice运行成功请求的XML子元素和子子元素。

在main2.rb中,我尝试使用方括号语法将文档从BookReservationRequest下移到Booker到UserWithoutALogin。 (这种方括号方法在响应方面的另一个例子中起作用)。不知怎的,我没有在Ruby中正确指定这些,因为当我在SOAP UI中删除这些标记时,我得到完全相同的错误消息。 (N.B我还是Ruby的新手!)

完整错误跟踪:

31/12/<TwoDigitYearMax>

Ruby代码(main2.rb):

ruby main2.rb
/Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/response.rb:85:in `raise_soap_and_http_errors!': (soap:Server) System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object. (Savon::SOAPFault)
   at Services.Internal.Helpers.CustomerHelper.GetCustomerIdFromGuestOrBookerType(Nullable`1 partnerServiceId, GuestOrBookerType guestOrBookerType) in c:\TeamCity\LB-QA-04\work\e2ec20c745b940f9\Source\Services\Internal\Helpers\CustomerHelper.cs:line 64
   at Services.Internal.Service.BookReservation(BookReservationRequest bookReservationRequest) in c:\TeamCity\LB-QA-04\work\e2ec20c745b940f9\Source\Services\Internal\Service.asmx.cs:line 289
   --- End of inner exception stack trace ---
    from /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/response.rb:14:in `initialize'
    from /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/operation.rb:72:in `new'
    from /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/operation.rb:72:in `create_response'
    from /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/operation.rb:58:in `call'
    from /Users/dan14/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/savon-2.11.1/lib/savon/client.rb:36:in `call'
    from main2.rb:11:in `core_info'
    from main2.rb:23:in `<main>'

SoapUI中的这个xml文档每次都会返回一个有效/成功的响应:

require 'savon'

class BookReservation

  def client
    client = Savon.client(wsdl: "http://some-example-soap-wsdl-link", follow_redirects: :follow_redirects)
  end

  def core_info(partner_code, restaurant_location_id, session_id, dining_date_and_time, size)
    message = { 'PartnerCode' => partner_code, 'RestaurantLocationId' => restaurant_location_id, 'SessionId' => session_id, 'DiningDateAndTime' => dining_date_and_time, 'Size' => size }
    response = client.call(:book_reservation, message: message)
    response.hash[:book_reservation_response]
  end

  def booker_info(first_name, last_name, email, guest_accepts_email_marketing)
    message = { 'FirstName' => first_name, 'LastName' => last_name, 'EMail' => email, 'GuestAcceptsEmailMarketingFromPartner' => guest_accepts_email_marketing }
    response = client.call([:book_reservation][:booker][:user_without_a_login], message: message)
    response.body[:book_reservation_response]
  end
end

  book = BookReservation.new
  puts book.core_info("DEV-DAN-BETH:73411", "10799", "DINNER", "2015-06-20T21:00:00", "2", )
  puts book.booker_info("John", "Smith", "john.smith@example.com", "true")

SoapUI中成功响应的示例:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://schemas.livebookings.net/OneFormat/Aggregator/Internal/1/0/">
    <soapenv:Header/>
    <soapenv:Body>      
        <ns:BookReservationRequest>            
            <ns:PartnerCode>DEV-DAN-BETH:73411</ns:PartnerCode>
            <ns:RestaurantLocationId>10799</ns:RestaurantLocationId>
            <ns:SessionId>DINNER</ns:SessionId>
            <ns:DiningDateAndTime>2015-06-20T21:00:00</ns:DiningDateAndTime>
            <ns:Size>2</ns:Size>
            <ns:Booker>
                <ns:UserWithoutALogin>
                    <ns:FirstName>John</ns:FirstName>
                    <ns:LastName>Smith</ns:LastName>
                    <ns:EMail>john.smith@example.com</ns:EMail>                      
                    <ns:GuestAcceptsEmailMarketingFromPartner>true</ns:GuestAcceptsEmailMarketingFromPartner>
               </ns:UserWithoutALogin>
            </ns:Booker>       
        </ns:BookReservationRequest>
    </soapenv:Body>
</soapenv:Envelope>

非常感谢!

1 个答案:

答案 0 :(得分:0)

您的SOAP消息示例包含一个Body,其中包含一个包含Booker块的Book Reservation请求。

但是,你的BookReservation类包含两个调用Savon客户端的方法 - 其中一个发送BookReservationRequest - 但是在发送之前你永远不会将Booker添加到消息哈希中。

几乎100%确定您的客户端发送了无效的SOAP请求,因为您尚未在SOAP正文中指定<ns:Booker>块。

我怀疑如果你更新了你的类以包含一个如下所示的方法:

def execute(partner_code, restaurant_location_id, session_id, dining_date_and_time, size, first_name, last_name, email, guest_accepts_email_marketing)
    message = { 'PartnerCode' => partner_code, 'RestaurantLocationId' => restaurant_location_id, 'SessionId' => session_id, 'DiningDateAndTime' => dining_date_and_time, 'Size' => size }
    message.merge!('Booker' => { 'FirstName' => first_name, 'LastName' => last_name, 'EMail' => email, 'GuestAcceptsEmailMarketingFromPartner' => guest_accepts_email_marketing })

    response = client.call(:book_reservation, message: message)
    response.hash[:book_reservation_response]

并称之为:

book.execute(
  "DEV-DAN-BETH:73411", "10799", "DINNER", "2015-06-20T21:00:00", "2",
   "John", "Smith", "john.smith@example.com", "true" )

你会有更好的运气。

相关问题