A tutorial on writing SOAP servers using Ruby's soap4r

Source: Internet
Author: User
Tags soap require soap client

This article mainly introduces the tutorial of using Ruby's soap4r to write SOAP server, and introduces the method of adding drive and invoke service in detail, the friend that need can refer to the following

What is SOAP?

Simple Object Access Protocol (SOAP) is a cross-platform, language-independent, xml-based RPC protocol, usually (but not necessarily) HTTP.

It uses XML to encode information to make remote procedure calls, HTTP to transfer information from client to server on the network, and vice versa.

SOAP has several advantages over other technologies, such as Com,corba, for example, its relatively inexpensive deployment and debugging costs, its scalability and ease of use, and the existence of several different language and platform implementations.

See a simple tutorial to learn about SOAP

This tutorial will familiarize you with the SOAP implementation Ruby (SOAP4R). This is a basic tutorial, so if you need in-depth details, you need to refer to other resources.

Install soap4r:

SOAP4R is set up by Hiroshi Nakamura to download the soap implementation of Ruby's development directly from the Web:

Note: It is possible that this component has been installed.

Download SOAP

If you know the Gem utility, you can use the following command to install the SOAP4R and related packages.

$ gem Install soap4r--include-dependencies

If you are working on Windows, you need to download a compressed file from the above location and need to install it using standard installation methods to run Ruby's install.rb.

To write a soap4r server:

SOAP4R supports two different types of servers:

CGI/FASTCGI based (soap::rpc::cgistub)

Standalone (Soap::rpc:standaloneserver)

This tutorial will write a separate server in detail. The following steps are involved in writing a SOAP server:

Step 1th-Inherit the Soap::rpc::standaloneserver class:

To implement your own stand-alone server, you need to write a new class that will soap::standaloneserver the subclass of the class, as follows:

Copy code code as follows:

Class MyServer < Soap::rpc::standaloneserver



Note: If you want to write a fastcgi server, you need to inherit the Soap::rpc::cgistub class, and the remaining steps will remain the same.

Step 2nd-Define Handler methods:

The second step is to write Web service methods that you want to expose to the outside world.

They can be written as simple ruby methods. For example, let's write a method of dividing two two two numbers and two numbers:


1 2 3 4 5 6 7 8 9 10 11 Class MyServer < Soap::rpc::standaloneserver .... # Handler Methods def add (A, b) return a + B-end def div (A, B) return

Step 3rd-Exposure handler method:

The next step is the method we define to add to our server. The Initialize method is used to expose a service in one of the following two ways:


1 2 3 4 5 Class MyServer < Soap::rpc::standaloneserver def initialize (*args) Add_method (receiver, MethodName, *paramarg) end End

The following parameter description:

To understand the usage of inout or out parameters, consider the following service method this takes two parameters (Inpar Am and Inoutparam), returns one normal return value (RetVal) and two further parameters:inoutparam and Outparam:


1 2 3 4 5 6 def ameth (Inparam, inoutparam) RetVal = Inparam + Inoutparam Outparam = Inparam. Inoutparam Inoutparam = Inparam * Inoutparam return retVal, Inoutparam, Outparam end

Now, we can disclose this method as follows:


1 2 3 4 5 6 Add_method (self, ' ameth ', [%w (in Inparam),%w (InOut inoutparam),%w (out Outparam),%w (RetVal return)]

Step 4th-Start the server:

The last step is to start the server with an instance of the derived class of the instance and call the Start method.


1 2 3 4 MyServer = myserver.new (' ServerName ', ' urn:ruby:ServiceName ', hostname, port) Myserver.start

This is a description of the required parameters:

For example:

Now using the steps above, let's write a separate server:


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28 Require "Soap/rpc/standaloneserver" Begin class MyServer < Soap::rpc::standaloneserver # expose our services def in Itialize (*args) Add_method (self, ' add ', ' a ', ' B ') Add_method (self, ' div ', ' a ', ' B ') End # Handler Methods def add (A, B) Return a + B-def div (A, b) return b/A (a) end server = Myserver.new ("MyServer", ' urn:ruby:calculation ', ' localhost ', 8080) trap (' INT) {Server.shutdown} Server.start rescue => err puts Err.message end

At execution time, the server application begins a separate soap service on the localhost to listen for port 8080 requests. It exposes a service method: Add and Div, which takes two parameters and returns the result.

You can now run this server backstage as follows:


1 $ Ruby myserver.rb&

Write SOAP4R client:

SOAP::RPC::D River class is used for writing to SOAP client applications for support. This tutorial will introduce this class to show the basics of the application that it uses.

The following is the minimum required information to invoke the SOAP service:

SOAP Service (SOAP endpoint URL)

Service method (method namespace URI)

Name of service method and its parameters

Now we will write a SOAP client to invoke the service definition method in the example above named Add and Div.

The following are the main steps to create a SOAP client:

Step 1-Create a SOAP driver instance:

We create an instance Soap::rpc::D River by calling the new method as follows:


1 SOAP::RPC::D river.new (EndPoint, NameSpace, SOAPAction)

This is a description of the required parameters:

Step 2nd-Ways to add a service:

To add to the SOAP Soap service method to Soap::rpc::D River We can invoke the following method to use Soap::rpc::D River instance:

Driver.add_method (name, *paramarg)

The following parameter description:

Step 3rd-Invoke the SOAP service:

The final step is to invoke the SOAP service to use SOAP::RPC::D River Instance as follows:


1 result = Driver.servicemethod (Paramarg ...)

Here Servicemethod is the actual Web service method and Paramarg ... Is the list parameter that needs to be passed in the service method.

For example:

Based on the above steps, we will write a SOAP client as follows:


#!/usr/bin/ruby-w   require ' soap/rpc/driver '   NAMESPACE = ' urn:ruby:calculation ' URL = ' http://localhost:80 80/'   BEGIN driver = Soap::rpc::D river.new (URL, NAMESPACE)   # Add Remote Sevice methods (' Add ', ' A ', ' B ')   # Call Remote service methods puts Driver.add rescue => err puts Err.message  
1 2 3 4 5 6 7 8 9 a
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.