<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   >
<channel>
    <title>Eduzine© - Java</title>
    <link>http://eduzine.edujini-labs.com/</link>
    <description>Articles from Edujini Team</description>
    <dc:language>en</dc:language>
    <admin:errorReportsTo rdf:resource="mailto:" />
    <generator>Serendipity 0.8.2 - http://www.s9y.org/</generator>
    <pubDate>Sun, 08 Oct 2006 16:35:27 GMT</pubDate>

    <image>
        <url>http://www.edujini-labs.com/images/newmasthead.gif</url>
        <title>RSS: Eduzine© - Java - Articles from Edujini Team</title>
        <link>http://eduzine.edujini-labs.com/</link>
        <width></width>
        <height></height>
    </image>
<item>
    <title>Consuming Web Services in Java 5</title>
    <link>http://eduzine.edujini-labs.com/archives/12-Consuming-Web-Services-in-Java-5.html</link>
<category>Java</category>    <comments>http://eduzine.edujini-labs.com/archives/12-Consuming-Web-Services-in-Java-5.html#comments</comments>
    <wfw:comment>http://eduzine.edujini-labs.com/wfwcomment.php?cid=12</wfw:comment>
    <slash:comments>0</slash:comments>
    <wfw:commentRss>http://eduzine.edujini-labs.com/rss.php?version=2.0&amp;type=comments&amp;cid=12</wfw:commentRss>
    <author>eduzine@edujinionline.com (Eduzine)</author>
    <content:encoded>

&lt;p&gt;
This article describes how to consume web services in Java 1.5.0 using the new JAX-WS 2.0 API (JSR 228 at http://jcp.org/en/jsr/detail?id=224).
&lt;/p&gt;
&lt;p&gt;
Developers around the world, including me, have always complained about the hard ways to work in Java to consume even a web service as simple as adding two numbers.
&lt;/p&gt;
&lt;p&gt;
However, with JAX-WS 2.0 API now available in core Java in JDK 1.5.0, life is simple like never before.
&lt;/p&gt;
&lt;p&gt;
The article describes how this API can be used for maximum benefits using some off-the-shelf tools similar to wsdl.exe available from Microsoft in the .Net Framework to generate the proxy classes. 
&lt;/p&gt;
 
&lt;p&gt;
Before starting with the discussion, some vocabulary background may be needed for the first timers. Following are simple definitions to the terms used:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;&lt;b&gt;Web Service&lt;/b&gt;: An operation / task available on a remote application running in an unknown environment.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;SOAP&lt;/b&gt;: A language in which the two unknown applications would talk to each other.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;Message&lt;/b&gt;: The content transferred over wire using SOAP as the language.&lt;/li&gt;
 &lt;li&gt;&lt;b&gt;WSDL&lt;/b&gt;: A language that would define the exact grammar of SOAP.&lt;/li&gt;

 &lt;li&gt;&lt;b&gt;Proxy Class&lt;/b&gt;: A piece of code, available on the client machine, that would perform the task of making connections to the server and taking care of serialization and de-serialization.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;Publishing a Web Service using ASP.Net:&lt;/h3&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;
For our case, we would publish a web service using ASP.Net. You can look into other articles around the globe onto how to publish a web service using ASP.Net.
&lt;/p&gt;
&lt;p&gt;
Here is the synopsis of the web service:
&lt;/p&gt;
&lt;pre&gt;Name: BasicWebServices&lt;br /&gt;Namespace: http://www.edujinionline.com/ns/ws/samples/dn/S01BasicServices/&lt;br /&gt;&lt;br /&gt;Operation: Add&lt;br /&gt;Input Message: AddMessage&lt;br /&gt;Output Message: AddMessageResponse&lt;br /&gt;AddMessage: Two parameters, x and y, of type double&lt;br /&gt;AddMessageResponse: One parameter, AddMessageResult, of type double&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;Consuming the Web Service using JAX-WS 2.0 in Java 1.5&lt;/h3&gt;
&lt;p&gt; &lt;/p&gt;

&lt;p&gt;
Download JAX-WS Reference Implementation from https://jax-ws.dev.java.net/jax-ws-20-fcs/. Extract the files using the procedure described in the installation section of the document above.
&lt;/p&gt;
&lt;p&gt;
Dig into the bin folder that would have a utility called wsimport. Yes! All those who have worked with wsdl.exe in .Net Framework would immediately understand what the purpose of the tool is
&lt;/p&gt;

&lt;p&gt;
Apart from various other options, the important options supported by the tool are:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;&lt;code&gt;-p pkg&lt;/code&gt;: Name of the package where the source files will be generated&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;-s src-directory&lt;/code&gt;: Path to the directory where the source files will be kept&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;-d directory&lt;/code&gt;: Path to the directory where the binary files will be kept&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
On the console, issue the following command (all in one line):
&lt;/p&gt;

&lt;pre&gt;wsimport&lt;br /&gt;    p com.edujinionline.tutorials.services&lt;br /&gt;    s src&lt;br /&gt;    d bin&lt;br /&gt;    http://localhost/BasicWebServices.asmx?WSDL&lt;/pre&gt;

&lt;p&gt;
The last parameter is the location of the WSDL file. Yes, it works with HTTP transport beautifully.
&lt;/p&gt;

&lt;p&gt;

Now now, what? The proxy class is available.
You will find the following classes generated:
&lt;/p&gt;&lt;ul&gt;
 &lt;li&gt;&lt;code&gt;BasicWebServices&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;BasicWebServicesSoap&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;AddMessage&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;AddMessageResponse&lt;/code&gt;&lt;/li&gt;
 &lt;li&gt;&lt;code&gt;ObjectFactory&lt;/code&gt;&lt;/li&gt;

&lt;/ul&gt;
&lt;p&gt;

&lt;/p&gt;&lt;p&gt;
... and these are the proxy classes ready for use. See the code below for demonstration.
&lt;/p&gt;

&lt;pre&gt;&lt;span class=&quot;cs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;class&lt;/span&gt; BasicWebServicesClient&lt;br /&gt;{&lt;br /&gt;    &lt;span class=&quot;cs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; testAdd(&lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; x, &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; y)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; retVal = &lt;span class=&quot;cs-literal&quot;&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        BasicWebServices service = &lt;span class=&quot;cs-keyword&quot;&gt;new&lt;/span&gt; BasicWebServices();&lt;br /&gt;        BasicWebServicesSoap soap = service.getBasicWebServicesSoap();&lt;br /&gt;&lt;br /&gt;        ObjectFactory factory = &lt;span class=&quot;cs-keyword&quot;&gt;new&lt;/span&gt; ObjectFactory();&lt;br /&gt;        AddMessage request = factory.createAddMessage();&lt;br /&gt;        request.setX(x);&lt;br /&gt;        request.setY(y);&lt;br /&gt;&lt;br /&gt;        AddMessageResponse response = soap.add(request);&lt;br /&gt;&lt;br /&gt;        retVal = response.getAddMessageResult();&lt;br /&gt;&lt;br /&gt;        &lt;span class=&quot;cs-keyword&quot;&gt;return&lt;/span&gt; retVal;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt;
And the main method to use the wrapper method &lt;code&gt;testAdd&lt;/code&gt; described above:
&lt;/p&gt;

&lt;pre lang=&quot;cs&quot;&gt;&lt;span class=&quot;cs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;class&lt;/span&gt; MainClass&lt;br /&gt;{&lt;br /&gt;    &lt;span class=&quot;cs-keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;cs-keyword&quot;&gt;void&lt;/span&gt; main(String[] args)&lt;br /&gt;    {&lt;br /&gt;        &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; x = &lt;span class=&quot;cs-literal&quot;&gt;34.56&lt;/span&gt;;&lt;br /&gt;        &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; y = &lt;span class=&quot;cs-literal&quot;&gt;45.67&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;        &lt;span class=&quot;cs-keyword&quot;&gt;double&lt;/span&gt; z = BasicWebServicesClient.testAdd(x, y);&lt;br /&gt;        System.&lt;span class=&quot;cs-keyword&quot;&gt;out&lt;/span&gt;.printf(&lt;span class=&quot;cpp-string&quot;&gt;&amp;quot;%f + %f = %f&amp;quot;&lt;/span&gt;, x, y, z);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt; &lt;/p&gt;

&lt;p&gt;
Consuming web services in Java requires just two steps now. The first step is to create proxy classes. Second step is to go ahead and use them.
&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;
&lt;h3&gt;Documentation, .Net Source et al&lt;/h3&gt;
&lt;p&gt; &lt;/p&gt;

&lt;p&gt;
You can download all code, documentation with systematic description on how to create the project from &lt;a href=&quot;http://www.edujinionline.com/downloads&quot;&gt;http://www.edujinionline.com/downloads&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
The article is also available at &lt;a href=&quot;http://www.codeproject.com/useritems/edujini_webservice_java.asp&quot;&gt;CodeProject&lt;/a&gt;.
&lt;/p&gt;

    </content:encoded>
    <pubDate>Tue, 10 Oct 2006 00:01:00 -0700</pubDate>
    <guid isPermaLink="false">http://eduzine.edujini-labs.com/archives/12-guid.html</guid>
    </item>
</channel>
</rss>
