Converting Java Object To SOAP Request And Response XML

To give you the basic idea about SOAP, the transport of messages in the SOAP and to point out the advantage and disadvantages of SOAP, I have brought the following text from Wikipedia. Following the text is my tutorial on how simply you can transfer a Java object to a SOAP XML and how to understand the SOAP response.

SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks. It relies on Extensible Markup Language (XML) as its message format, and usually relies on other Application Layer protocols (most notably Remote Procedure Call (RPC) and HTTP) for message negotiation and transmission. SOAP can form the foundation layer of a web services protocol stack, providing a basic messaging framework upon which web services can be built.

As a layman’s example of how SOAP procedures can be used, a SOAP message could be sent to a web service enabled web site (for example, a house price database) with the parameters needed for a search. The site would then return an XML-formatted document with the resulting data (prices, location, features, etc). Because the data is returned in a standardized machine-parseable format, it could then be integrated directly into a third-party site.

The SOAP architecture consists of several layers of specifications for message format, message exchange patterns (MEP), underlying transport protocol bindings, message processing models, and protocol extensibility. SOAP is the successor of XML-RPC, though it borrows its transport and interaction neutrality and the envelope/header/body from elsewhere (probably from WDDX).

SOAP makes use of an Internet application layer protocol as a transport protocol. Critics have argued that this is an abuse of such protocols, as it is not their intended purpose and therefore not a role they fulfill well. Proponents of SOAP have drawn analogies to successful uses of protocols at various levels for tunneling other protocols.

Both SMTP and HTTP are valid application layer protocols used as Transport for SOAP, but HTTP has gained wider acceptance as it works well with today’s Internet infrastructure; specifically, HTTP works well with network firewalls. SOAP may also be used over HTTPS (which is the same protocol as HTTP at the application level, but uses an encrypted transport protocol underneath) with either simple or mutual authentication; this is the advocated WS-I method to provide web service security as stated in the WS-I Basic Profile 1.1. This is a major advantage over other distributed protocols like GIOP/IIOP or DCOM which are normally filtered by firewalls. SOAP over AMQP is yet another possibility, that some implementations support.

XML was chosen as the standard message format because of its widespread use by major corporations and open source development efforts. Additionally, a wide variety of freely available tools significantly eases the transition to a SOAP-based implementation. The somewhat lengthy syntax of XML can be both a benefit and a drawback. While it promotes readability for humans, facilitates error detection, and avoids interoperability problems such as byte-order (Endianness), it can retard processing speed and be cumbersome. For example, CORBA, GIOP, ICE, and DCOM use much shorter, binary message formats. On the other hand, hardware appliances are available to accelerate processing of XML messages.Binary XML is also being explored as a means for streamlining the throughput requirements of XML.

Advantages of SOAP:

  • Using SOAP over HTTP allows for easier communication through proxies and firewalls than previous remote execution technology.
  • SOAP is versatile enough to allow for the use of different transport protocols. The standard stacks use HTTP as a transport protocol, but other protocols are also usable (e.g., SMTP).
  • SOAP is platform independent.
  • SOAP is language independent.

Disadvantages

  • Because of the verbose XML format, SOAP can be considerably slower than competing middleware technologies such as CORBA. This may not be an issue when only small messages are sent. To improve performance for the special case of XML with embedded binary objects, the Message Transmission Optimization Mechanism was introduced.
  • When relying on HTTP as a transport protocol and not using WS-Addressing or an ESB, the roles of the interacting parties are fixed. Only one party (the client) can use the services of the other. Developers must use polling instead of notification in these common cases.
  • Most uses of HTTP as a transport protocol are done in ignorance of how the operation would be modeled in HTTP.[citation needed] This is by design—similar to how different protocols sit on top of each other in the IP stack. But this analogy is imperfect; the application protocols used as transport protocols aren’t really transport protocols. As a result, there is no way to know if the method used is appropriate to the operation. This makes good analysis at the application-protocol level problematic with sub-optimal results—for example, a POST operation is used when it would more naturally be modeled as a GET. The REST architecture has become a web service alternative that makes appropriate use of HTTP’s defined methods.
  • When relying on HTTP as a transport protocol, a firewall designed to only allow web browsing is forced to perform more detailed (and thus less efficient) analysis of the HTTP packages.[clarification needed]
  • Although SOAP is an open standard, not all languages offer appropriate support. Java, .NET and Flex offer excellent SOAP integration and/or IDE support. Python and PHP support is much weaker.

— Source:  Wikipedia /Creative Commons Attribution-ShareAlike License

SOAP uses XML as the data-encoding format. XML-RPC and ebXML use XML as well.

Consider the following Java interface:

public interface SumMyNumbers
{
public int sum(int num1, int num2);
}

A client calling the sum() method with two integer parameters would expect to receive a sum of integers from the server. The request format to send to the server would be some XML data.

<?xml version="1.0"?>;
<SumMyNumbers>;
 <sum>
   <num1>2</num1>
   <num2>13</num2>
 </sum>
</SumMyNumbers>

If you notice the XML above, the interface name has been made the root node. There are also nodes for method and parameter names. Now we must deliver this request to the server. Instead of creating our own TCP/IP protocol, we’ll defer to HTTP. So, the next step is to package the request into the form of an HTTP POST request and send it to the server. The server receives the request, decodes the XML, and sends the client a response, again in the form of XML. For instance, the response might look like below

<?xml version="1.0"?>
 <SumMyNumbers>
  <sumResponse>
  <sumValue>15</sumValue>
 </sumResponse>
</SumMyNumbers>

The root node is still the interface name SumMyNumbers. But this time, instead of just the method name, the node name, sum, is the method name plus the string Response, making the node as sumResponse. Using the method name plus Response word, the client can easily identify which method was called and the response is to what method.

The following show how the same request above is encoded in SOAP:

<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Header></SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:sum xmlns:ns1="SumMyNumbers" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<num1 xsi:type="xsd:int">2</num1>
<num2 xis:type="xsd:int">13</num2>
</ns1:sum>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The following diagram shows the basic functioning of SOAP architecture.

Read my another article Complete Tutorial On Using SOAP-UI to Mock Web Service Request / Response

Complete Tutorial On Using SOAP-UI to Mock Web Service Request / Response

I found SOAP-UI tool while I was looking for ways to mock the web services temporarily while I and my team were waiting on the real web-service to be ready for doing integration test with our front end application. SoapUI is an open source web service testing application for service-oriented architectures (SOA). Its functionality covers web service inspection, invoking, development, simulation and mocking, functional testing, load and compliance testing.

This tutorial covers some basic stuffs of using this tool to create Mock request/response from sample WSDL (Web Service Description Language). Although the tool offers advanced options and features, this scope of this article would be enable the readers to download, install and run a mock service using a simple WSDL file.

Here is the WSDL file that I will be using in this tutorial.

<definitions name="SampleService" targetNamespace="http://www.sanjaal.com/wsdl/SampleService.wsdl" 
	xmlns="http://schemas.xmlsoap.org/wsdl/" 
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
	xmlns:tns="http://www.sanjaal.com/wsdl/SampleService.wsdl" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">

	<message name="SampleRequest">
		<part name="firstName" type="xsd:string"/>
	</message>
	<message name="SampleResponse">
		<part name="greeting" type="xsd:string"/>
	</message>
	<portType name="Sample_PortType">
		<operation name="sampleOperation">
			<input message="tns:SampleRequest"/>
			<output message="tns:SampleResponse"/>
		</operation>
	</portType>
	<binding name="SampleBinding" type="tns:Sample_PortType">
		<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
		<operation name="sampleOperation">
			<soap:operation soapAction="sampleOperation"/>
			<input>
				<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
					namespace="urn:sanjaal:sample-service" use="encoded"/>
			</input>
			<output>
				<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
					namespace="urn:sanjaal:sample-service" use="encoded"/>
			</output>
		</operation>
	</binding>
	<service name="Sample_Service">
		<documentation>WSDL File for SampleService</documentation>
		<port binding="tns:Sample_Binding" name="Sample_Port">
			<soap:address location="http://www.sanjaal.com/sample-service"/>
		</port>
	</service>
</definitions>


Download SOAP-UI:

SOAP UI tool is free and open source tool and can be downloaded from the website www.soapui.org. I prefer to download in the zip format, which you can just unzip to any location of your choice and are ready to start to work with. But they have other binary distributions with installers. No matter which option you use, there will be folder where SOAP UI is installed or unzipped.

As you can see in the screenshot below, I had unzipped the SOAP-UI tool to C:soapui-4.5.1 folder.

To start the tool, navigate to the bin folder and double click on soapui.bat file. You can also run this bat file from command prompt.

SOAP-UI Webservice Mock Tutorial

Once the bat file runs, you will see a blank Soap-UI tool running.
SOAP-UI Webservice Mock Tutorial

To start a new project, click on File > New soapUI Project or just hit Ctrl + N
SOAP-UI Webservice Mock Tutorial

You will be prompted with a New soapUI Project dialog box where you can provide a name for your project. You will also see that you can browse your WSDL file using this dialog.
SOAP-UI Webservice Mock Tutorial

Go ahead and browse your WSDL file. If you don’t have a WSDL file, you can copy the content of the sample WSDL we have provided at the beginning of this tutorial and save it to a file as .wsdl extension – such as sample.wsdl
SOAP-UI Webservice Mock Tutorial

Once the WSDL file is loaded, you will see something similar to the following. The view depends on how many operations you have defined in the WSDL file, what names you have chosen etc. For each operation, you will see the Requests created by default.
SOAP-UI Webservice Mock Tutorial

To mock a service for this request, right click on the Binding (again depends on what names you choose for your binding), and click on Generate Mock Service
SOAP-UI Webservice Mock Tutorial

You will be prompted with a Generate MockService dialog where you can choose operations, path (which will be a part of your endpoint definition) and you can even tell the tool what port to use for this service. Make sure to choose the port that has already not been taken.
SOAP-UI Webservice Mock Tutorial

Once you hit OK on the above dialog,  it will ask for what you want to name this WebService Mock. Give it a name – it can be anything.
SOAP-UI Webservice Mock Tutorial

You will then see that on the navigator, a MockService with the name that you provided on the prior screenshot is created. When you double clock on the operation name of your choice, it will open a window on the right hand side where you have option to run the Mock Service (The small green icon). Click on that icon to start the MockService
SOAP-UI Webservice Mock Tutorial

The following window shows that the MockService is running (red button stops the service), it also displays what port it is running on.
SOAP-UI Webservice Mock Tutorial

Double click on the request from the left hand navigation. If you want to change the end point, you can edit the URL on this screen. Once you have selected the right end point, click on the small green icon on the request window.
SOAP-UI Webservice Mock Tutorial

If the mock service is running correctly, you will see the response on the right window.
SOAP-UI Webservice Mock Tutorial
Once the service is running, you can also connect to it from your browser. My endpoint is defined at: http://localhost:8088/sampleService which is my webservice URL.

Just type in the url without the service name (in my case I typed just http://localhost:8088). You will see a page seeing what all services are running.
SOAP-UI Webservice Mock Tutorial

If you click on the service of your choice, you will be shown with the WSDL.
SOAP-UI Webservice Mock Tutorial

You have successfully completed learning how to mock a webservice using SOAP-UI.