Monday, July 22, 2013

Java Web Service Tutorial (With SOAP UI)- Part 03


  In the last part (02) we saw about SOAP elements, SOAP Specifications and  SOAP Faults.
  Now we will see SOAP encoding : 

 - Soap includes a built in set of rules for encoding Data types. 
 - These data types can be float,integers,String,Arrays etc
         
        They are two SOAP Data types :  
        1. Scalar 2. Compound


  1. Scalar : Scalar types contains only one value such as "Last Name","Price" etc.

  2.Compound : Compound Types contains multiple values such as "Purchase of orders" ,"List of Stock  Quotes."Compound types are further subdivided into arrays and structs. Arrays contain multiple values,  each of which is specified by an ordinal position. Structs also contain multiple values, but each element is    specified by an accessors name.

-  The encoding style for a SOAP message is set via the SOAP-ENV:encodingStyle attribute.
-  To use SOAP 1.1 encoding, use the value http://schemas.xmlsoap.org/soap/encoding/.
-  To use SOAP 1.2 encoding, use the value http://www.w3.org/2001/09/soap-encoding.

Ex: Scalar Type

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2001/09/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getPriceResponse
xmlns:ns1="urn:examples:priceservice"
SOAP-ENV:encodingStyle="http://www.w3.org/2001/09/soap-encoding">
<return xsi:type="xsd:double">54.99</return>

</ns1:getPriceResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

- The encoding Style Attribute is used to set the Encoding Style.
- Scalar types includes int,short,long,double,boolean,float,date,time etc.
- The SOAP specification provides several options for indicating the data type of a specific XML element.        The first option is to specify an xsi:type attribute for each element. The second option is to store data type      information within an external XML Schema or even within human-readable documentation.

Ex: Compound Type (Array)

Here is a sample SOAP response with an array of double values:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2001/09/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getPriceListResponse
xmlns:ns1="urn:examples:pricelistservice"
SOAP-ENV:encodingStyle="http://www.w3.org/2001/09/soap-encoding">
<return
xmlns:ns2="http://www.w3.org/2001/09/soap-encoding"
xsi:type="ns2:Array" ns2:arrayType="xsd:double[2]">
<item xsi:type="xsd:double">54.99</item>
<item xsi:type="xsd:double">19.99</item>
</return>

</ns1:getPriceListResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Here , Array size is declared as 2, so two values has been send in the response for the Array.

Compound Type (Struct)

In contrast to arrays, structs contain multiple values, but each element is specified with a unique accessor element.
For example, consider an item within a product catalog. In this case, the struct might contain a product SKU, product name, description, and price. Here is how such a struct would be represented in a SOAP message:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2001/09/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getProductResponse
xmlns:ns1="urn:examples:productservice"
SOAP-ENV:encodingStyle="http://www.w3.org/2001/09/soap-encoding">
<return xmlns:ns2="urn:examples" xsi:type="ns2:product">
<name xsi:type="xsd:string">Red Hat Linux</name>
<price xsi:type="xsd:double">54.99</price>
<description xsi:type="xsd:string">
Red Hat Linux Operating System
</description>
<SKU xsi:type="xsd:string">A358185</SKU>
</return>

</ns1:getProductResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Each element in a struct is specified with a unique accessor name.
For example, the message above includes four accessor elements: name , price , description , and SKU. Each element can have its own data type; for example, name is specified as a string , whereas price is specified as a double.

Literal Encoding:

We are not required to use the SOAP encoding style. In fact,occasionally you may want to ignore the SOAP encoding rules completely and embed an entire XML document (or just a portion of the document) directly into your SOAP message.

Doing so is referred to as literal XML encoding, and it requires that you specify a literal XML encoding style. Within Apache SOAP, the literal XML style is specified with the namespace http://xml.apache.org/xml-soap/literalxml.

For example, the following is a second option for encoding product information. Rather than encoding the product as a SOAP struct, the data is encoded as a literal XML document:

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2001/09/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getProductResponse
xmlns:ns1="urn:examples:XMLproductservice"
SOAP-ENV:encodingStyle=
"http://xml.apache.org/xml-soap/literalxml">

<return>
<product sku="A358185">
<name>Red Hat Linux</name>
<description>Red Hat Linux Operating System</description>
<price>54.99</price></product>
</return>

</ns1:getProductResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

So this ends the SOAP Part of Webservice, So far we have learnt about SOAP Specifications,SOAP   Messaging elements, SOAP Faults and SOAP Encoding.All these will help to understand the SOAP Request even without writing the code, and helps to write our own soap request and test it in the SOAP Tools. 

One of the major tool use for testing web service is "SoapUI". Now test a sample service withour SOAP knowledge in SOAP UI.For this we need to install SOAP UI Software.

Download SOAP UI from www.sopaui.com . It has "2" version pro version which is not a free ware and SOAP UI open source version which is a free ware ,You can download any one of these.

We are going to test the Sample Webservice provided by w3schools.com called "TempConversion" which has the below wsdl url .

WSDL URL :http://www.w3schools.com/webservices/tempconvert.asmx?WSDL

This service accepts accept Celsius as "String" and returns the Fahrenheit as "String".If you hit the following URL in browser an XML document will be open which is called as "WSDL" document .Dont worry if you dont understand we will discuss WSDL in our next post in detail.

After installation Open the SOAP UI :

1. Create New SoapUI Project by right clicking on the Project Node:

















2. Copy the WSDL URL and paste in the Initial WSDL/WADL Text Box , and the Project Name will automatically show up and click "ok" button.














3. Now Double click on the "Request Node" Inside the "CelciusTo Fahrenheit " Node.A request window will be open with default Soap Request.

















Request :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:CelsiusToFahrenheit>
<!--Optional:-->
<tem:Celsius>122</tem:Celsius> <!-- Change the "? " to 122 in the Request -->
</tem:CelsiusToFahrenheit>
</soapenv:Body>
</soapenv:Envelope>

Mention a String value in the Celsius Tag and click the Run Button , You can see the Response in the Right Side.












Response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CelsiusToFahrenheitResponse xmlns="http://tempuri.org/">
<CelsiusToFahrenheitResult>251.6</CelsiusToFahrenheitResult>
</CelsiusToFahrenheitResponse>
</soap:Body>
</soap:Envelope>

If we  see the Request we can see the Envelope,Header,Body with Method and Parameters is present which we discuss in earlier parts, So now we can easily understand any SOAP Request now.

In this way , we can test our web service without writing client code, In the next post we will discuss about WSDL and it's contents.

<< Previous                                 Index                                   Next>>                
                           
                          --------------------------------------------------------------------------------------

Please comment your thoughts...



Technology Blogs

blogs

5 comments:

  1. Good Tutorial...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Easy to follow...

    ReplyDelete
  4. Hi,The publishing and transaction environments that sit behind the site are sympathetic in Web Design Cochin to the needs of the business management processes as well as the needs of those who must support and maintain the site.Thanks............

    ReplyDelete

  5. Hurrah, that?s what I was looking for, what a information! present here at this weblog, thanks admin of this web page. by ACC 290 Final Exam

    ReplyDelete