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

22 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
  6. I must thank you for the efforts you have put in writing this website.
    I'm hoping to see the same high-grade content by you in the future as well.

    In truth, your creative writing abilities has encouraged
    me to get my very own website now ;)

    Also visit my blog ... New Balance Store

    ReplyDelete
  7. Admiring the time and effort you put into your website
    and in depth information you present. It's nice to come across a blog every once in a while
    that isn't the same outdated rehashed information. Fantastic read!
    I've bookmarked your site and I'm adding your RSS feeds to my Google
    account.

    Also visit my weblog ... Dr Dre Beats Tour

    ReplyDelete
  8. Good way of explaining, and good article to obtain information about my presentation focus, which i am going to
    present in academy.

    Here is my webpage ... Cheap Dr Dre Beats

    ReplyDelete
  9. We are a group of volunteers and starting a brand new scheme in our community.
    Your site provided us with helpful information to work on. You've performed an impressive job and our whole community will be thankful to you.


    Feel free to surf to my webpage - Beats Monster

    ReplyDelete
  10. My spouse and I stumbled over here from a different web address
    and thought I might as well check things out. I
    like what I see so i am just following you. Look forward to
    looking into your web page for a second time.

    My web site: Christian Louboutin Boots

    ReplyDelete
  11. This is my first time go to see at here and i am really impressed to read everthing at
    single place.

    My web page Christian Louboutin Pumps

    ReplyDelete
  12. Heya i am for the first time here. I found this board and I
    find It really useful & it helped me out much.
    I hope to give something back and help others like you
    aided me.

    my web blog - Monster Beats

    ReplyDelete
  13. I blog frequently and I truly appreciate your information. Your
    article has truly peaked my interest. I am going to take a note of your site and keep checking for new information about once per week.

    I opted in for your RSS feed as well.

    Look at my site - Running Shoes

    ReplyDelete
  14. Right here is the right webpage for anyone who hopes to find out about this topic.
    You understand so much its almost hard to argue with you (not that
    I really will need to…HaHa). You definitely put a fresh spin on a subject which
    has been discussed for ages. Excellent stuff, just wonderful!


    my blog; Louis Vuitton Outlet UK

    ReplyDelete
  15. I think this is among the most important info for me.
    And i'm glad reading your article. But want to remark on few general
    things, The web site style is wonderful, the articles is really nice : D.
    Good job, cheers

    my website ... Louis Vuitton Sale

    ReplyDelete
  16. May I simply say what a comfort to find a person that
    genuinely knows what they are discussing over the internet.
    You actually know how to bring a problem to light and make
    it important. More and more people must look at this and understand this side of your
    story. It's surprising you are not more popular given that you most certainly have the gift.


    Look at my homepage: Cheap Louis Vuitton

    ReplyDelete
  17. I'd like to thank you for the efforts you have
    put in writing this site. I really hope to view the same high-grade blog posts from you later
    on as well. In fact, your creative writing abilities has motivated me to get my own blog now ;)

    Also visit my web site Louis Vuitton Outlet [http://louisvuittonaustralia-sale.onnistu.com/]

    ReplyDelete
  18. Hey There. I discovered your blog using msn. That
    is a very smartly written article. I'll make sure to bookmark it and come back to learn more of your helpful info.
    Thanks for the post. I'll definitely return.

    my web page :: Discount Christian Louboutin

    ReplyDelete
  19. Incredible points. Solid arguments. Keep up the great effort.



    my web site Louis Vuitton Online

    ReplyDelete
  20. Hey there! I'm at work browsing your blog from my new iphone 3gs!
    Just wanted to say I love reading your blog and look forward
    to all your posts! Keep up the outstanding work!


    Look into my homepage: Cheap Christian Louboutin

    ReplyDelete
  21. This article provides clear idea in favor of the new people
    of blogging, that really how to do blogging.

    my web page :: Christian Louboutin

    ReplyDelete
  22. I hardly create comments, but i did a few searching and wound up here "Java Web Service Tutorial (With SOAP UI)- Part 03".
    And I do have a couple of questions for you if it's allright.
    Could it be simply me or does it look as if like some of these responses
    look like they are coming from brain dead people?
    :-P And, if you are writing at other online social sites,
    I would like to follow everything fresh you have
    to post. Could you list of every one of all your shared pages like your twitter feed, Facebook page or
    linkedin profile?

    My site ... Running Shoes

    ReplyDelete