<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title>Java Programming Forum - Learn Java Programming - Blogs - Web Service</title>
		<link>http://www.java-forums.org/blogs/web-service/</link>
		<description>Java Programming Forum - Learning Java easily</description>
		<language>en</language>
		<lastBuildDate>Thu, 23 May 2013 19:42:02 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>http://www.java-forums.org/images/misc/rss.jpg</url>
			<title>Java Programming Forum - Learn Java Programming - Blogs - Web Service</title>
			<link>http://www.java-forums.org/blogs/web-service/</link>
		</image>
		<item>
			<title>Architecture of Web Service</title>
			<link>http://www.java-forums.org/blogs/web-service/1553-architecture-web-service.html</link>
			<pubDate>Sat, 07 Jul 2012 17:43:09 GMT</pubDate>
			<description>Following diagram explains the architecture of the web servers. 
 
Attachment 3939 (http://www.java-forums.org/attachments/jobs-offered/3939-websphere-portal-developers-indiana-1.jpg) 
...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Following diagram explains the architecture of the web servers.<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/jobs-offered/3939d1341682915-websphere-portal-developers-indiana-1.jpg" border="0" alt="Name:  1.jpg
Views: 5691
Size:  29.1 KB" class="thumbnail" style="float:CONFIG" /></div><br />
<b><div style="text-align: center;">Web Service Architecture</div></b><br />
<br />
<b>•	Web Service Discovery: </b>All the web services are discovered with the help of this part by meeting certain requirements and passing filters.  The UDDI which stands for Universal Description, Discovery and Integration handles this part of web services.<br />
<br />
<b>•	Web Service Description:</b> All the web services are self describing which is most important and interesting feature. A web service can be described once it is located and it will describe about its operations. This part is handled by the Web Services Description Language (WSDL).<br />
<br />
<b>•	Web Service Invocation:</b> The message handling between the web services and calling methods of a web service is called web service invocation. SOAP protocol is used to communicate with a web service.<br />
<br />
<b>•	Web Service Transport:</b> The entire message transmission between client and server must be transmitted by some mean. The HTTP protocol is used to do this message transmission.</blockquote>

]]></content:encoded>
			<dc:creator>Web Service</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/web-service/1553-architecture-web-service.html</guid>
		</item>
		<item>
			<title>SOAP - Envelope Element</title>
			<link>http://www.java-forums.org/blogs/web-service/1552-soap-envelope-element.html</link>
			<pubDate>Sat, 07 Jul 2012 17:40:49 GMT</pubDate>
			<description>The end and the start of the message are indicated by SOAP envelope. The receiver will be informed of the complete message arrival.  When a message...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">The end and the start of the message are indicated by SOAP envelope. The receiver will be informed of the complete message arrival.  When a message is received and processed, it will be informed by the SOAP envelope. So it solved the major problem of informing message processing. So in other words we can say that is a packaging method.<br />
<br />
Following points will explain it in more detail:<br />
<br />
<ul><li style="">A root element is the base of SOAP envelop message and it is contained by all the messages.</li><li style="">Only single Body element must be part of the Envelope element.</li><li style="">Every SOAP message must contain the Envelope element.</li><li style="">There should be only one Header element in the Envelope and it should be before the body part of Envelope as a first child.</li><li style="">If the SOAP version is changed, the envelope must change.</li><li style="">The “ENV” namespace is used to specify namespace for SOAP envelope.</li><li style="">The “Envelopeelement” is used to specify the SOAP envelope.</li><li style="">The “optionalencodingStyle” element and namespace name are used to declare SOAP encoding.</li><li style="">The v1.1 and v1.2 messages are not compatible with each other and it will generate a fault on message.</li></ul></blockquote>

]]></content:encoded>
			<dc:creator>Web Service</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/web-service/1552-soap-envelope-element.html</guid>
		</item>
		<item>
			<title>SOAP Introduction</title>
			<link>http://www.java-forums.org/blogs/web-service/1551-soap-introduction.html</link>
			<pubDate>Sat, 07 Jul 2012 17:39:49 GMT</pubDate>
			<description>To exchange messages and information between multiple computers, XML based protocol is used which is called SOAP. SOAP is XML based protocol so you...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">To exchange messages and information between multiple computers, XML based protocol is used which is called SOAP. SOAP is XML based protocol so you can say that it is the XML specification application.<br />
<br />
Following rules or statements are considered as true for the SOAP.<br />
<br />
•	It is a protocol for communication.<br />
•	SOAP stands for Simple Object Access Protocol.<br />
•	It is designed to use internet for all its communication.<br />
•	It has the facility to use HTTP for sending XML messaging.<br />
•	It handles data transportation of web services.<br />
•	A remote procedure may be called using this protocol.<br />
•	It can also be used to send documents.<br />
•	It can also be used for message broadcasting.<br />
•	It is independent of language.<br />
•	It is independent of platform.<br />
•	It is based on XML to send and get information and define how it will be achieved.<br />
<br />
SOAP is used in:<br />
<br />
•	Different message systems.<br />
•	Transport protocols<br />
•	Remote procedure calls.<br />
<br />
The major focus of SOAP is on the remote procedure calls using HTTP. It helps different applications to connect and method calling remotely.</blockquote>

]]></content:encoded>
			<dc:creator>Web Service</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/web-service/1551-soap-introduction.html</guid>
		</item>
		<item>
			<title>How to create a Java web service</title>
			<link>http://www.java-forums.org/blogs/web-service/1145-how-create-java-web-service.html</link>
			<pubDate>Wed, 04 Apr 2012 03:57:03 GMT</pubDate>
			<description>Web service is a XML-based standard that allows interoperability between different applications on different platforms, for example, a .NET based...</description>
			<content:encoded><![CDATA[<blockquote class="blogcontent restore">Web service is a XML-based standard that allows interoperability between different applications on different platforms, for example, a .NET based application on Windows can communicate with a Java based one on Linux. The communication can be done through a set of XML messages over HTTP protocol.<br />
<br />
Throughout this article, you will learn how to create and publish a web service, and consume the web service by a command line client. This article supposes you are familiar with basic Java programming using Eclipse IDE, as well as Ant build script at fundamental level.<br />
<br />
<br />
<b><font size="3">Java web service API</font></b><br />
<br />
Java API for XML Web Services (JAX-WS) has been included in the Java SE platform since version 6. That means, prior to Java SE 6, JAX-WS is only available for the Java EE platform, so developers could not write web services aware applications without Java EE runtime. However, JAX-WS is now a part of the Java SE platform.<br />
The API comprises two main packages:<br />
<br />
<ul><li style="">    <span style="font-family: Courier New"><font size="2">javax.xml.ws</font></span>: contains the core JAX-WS APIs.</li><li style="">    <span style="font-family: Courier New"><font size="2">javax.jws</font></span>: contains annotation classes that describe web service metadata. Annotations help writing web services based applications easily.</li></ul><br />
<br />
<br />
In addition, the Java SE platform provides some useful tools for generating web services artifacts: <span style="font-family: Courier New"><font size="2">wgen, wsimport, schemagen</font></span> and <span style="font-family: Courier New"><font size="2">xjc</font></span>. In this article, you will use the <span style="font-family: Courier New"><font size="2">wsimport</font></span> tool for generating Java classes for web services client.<br />
<br />
<br />
<b><font size="3">Create a web service class</font></b><br />
<br />
<ul><li style="">    Create a new Java project in Eclipse by selecting <b>File</b> -&gt; <b>New Project</b>, name the project as <i>MyWebService</i>.</li><li style="">    Create a source package called <span style="font-family: Courier New"><font size="2">com.mycompany.service</font></span>.</li><li style="">    Create a new, empty Java class in the above package, name it as <span style="font-family: Courier New"><font size="2">HelloWeb.java</font></span>. You should end up with a screen like this:</li></ul><br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3402d1333509395-how-extract-info-sentence-create-project.png" border="0" alt="Name:  create project.png
Views: 24025
Size:  43.9 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Create a web service class</b></div><br />
    The class is empty now. We will mark it as a web service by adding <span style="font-family: Courier New"><font size="2">@WebService</font></span> annotation to the class and implement a web service method by using <span style="font-family: Courier New"><font size="2">@WebMethod</font></span> annotation. Update source code of the class like the following:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: Web service class</div>

	<pre class="brush: java">package com.mycompany.service;

import javax.jws.WebMethod;

import javax.jws.WebService;

@WebService

public class HelloWeb {

   

    @WebMethod

    public String sayGreeting(String name) {

        return &quot;Greeting &quot; + name + &quot;!&quot;;

    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 Both the annotation types are from package <span style="font-family: Courier New"><font size="2">javax.jws</font></span>. The <span style="font-family: Courier New"><font size="2">@WebService</font></span> annotation marks a class as implementing a web service, and the <span style="font-family: Courier New"><font size="2">@WebMethod</font></span> annotation is used to expose a method as web service method.<br />
<br />
And we are done creating a web service class, pretty simple!<br />
<br />
<br />
<b><font size="3">Create a server class</font></b><br />
<br />
Instead of deploying the web service on a heavy weight Java EE application server such as Tomcat or Glassfish, we can deploy it on a built-in, lightweight server with the help of <span style="font-family: Courier New"><font size="2">javax.xml.ws.Endpoint</font></span> class.<br />
<br />
In Eclipse, create a new class called Server.java under the package <span style="font-family: Courier New"><font size="2">com.mycompany.service</font></span> with a <span style="font-family: Courier New"><font size="2">main()</font></span> method, and just add two lines of code to the <span style="font-family: Courier New"><font size="2">main()</font></span> method:<br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: Server class</div>

	<pre class="brush: java">package com.mycompany.service;

import javax.xml.ws.Endpoint;

public class Server {

    public static void main(String&#91;&#93; args) {

        Endpoint.publish(&quot;http://localhost:9898/HelloWeb&quot;, new HelloWeb());

        System.out.println(&quot;HelloWeb service is ready&quot;);

    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 <br />
The static method <span style="font-family: Courier New"><font size="2">publish(String address, Object implementor)</font></span> creates and publishes a web service at a specified URL. In the code above:<br />
<br />
<ul><li style="">    A local server is created to listen at the port 9898.</li><li style="">    A new instance of <span style="font-family: Courier New"><font size="2">HelloWeb</font></span> is created and bind to the URL <i>/HelloWeb</i></li></ul><br />
<br />
<br />
The server code is done, and we are ready to test the web service now.<br />
<br />
<br />
<b><font size="3">Publish the web service</font></b><br />
<br />
    Run the <span style="font-family: Courier New"><font size="2">Server</font></span> class as a standalone Java application by selecting <b>Run -&gt; Run As -&gt;  Java Application</b> from Eclipse’s main menu. Once the server is started, you should see the following message in <i>Console </i>view:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3403d1333509770-how-extract-info-sentence-server-started.png" border="0" alt="Name:  server started.png
Views: 23799
Size:  12.0 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: the web service is published on a built in server</b></div><ul><li style="">    We now try to access the server. Select<b> Window -&gt; Show View -&gt; Other</b>, select <i>General – Internal Web Browser</i>, to open an internal browser in Eclipse.<br /></li><li style="">    Type the following line into address bar of the internal browser:<br />
<br />
<div style="text-align: center;"><i><a href="http://localhost:9898/HelloWeb?wsdl" target="_blank" rel="nofollow">http://localhost:9898/HelloWeb?wsdl</a></i></div></li><li style="">    The server should return an XML document as in the following screen-shot:</li></ul><br />
<br />
<div style="text-align: center;"><a href="http://www.java-forums.org/attachments/xml/3404d1333509913-how-extract-info-sentence-wsdl-xml.png" id="attachment3404" rel="Lightbox_1145" target="_blank" ><img src="http://www.java-forums.org/attachments/xml/3404d1333509913t-how-extract-info-sentence-wsdl-xml.png" border="0" alt="Click image for larger version.&nbsp;

Name:	wsdl xml.png&nbsp;
Views:	1313&nbsp;
Size:	164.3 KB&nbsp;
ID:	3404" class="thumbnail" style="float:CONFIG" /></a><br />
<b>Figure: The WSDL  document returned from the server</b></div><br />
The WSDL document describes a web service in XML format, allows other applications to discover and consume web service methods as per described by the WSDL.<br />
<br />
<br />
<b><font size="3">Invoke the web service method</font></b><br />
<br />
Now we are trying to invoke the <span style="font-family: Courier New"><font size="2">sayGreeting()</font></span> method from web interface in Eclipse.<br />
<br />
    Make sure you have the <i>Java EE perspective</i> is active in Eclipse. Select <b>Run -&gt; Run the Web Services Explorer</b>. The <i>Web Services Explorer</i> view is opened:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3405d1333510040-how-extract-info-sentence-web-services-epxplorer.png" border="0" alt="Name:  web services epxplorer.png
Views: 23841
Size:  27.1 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Open Web Services Explorer View</b></div><br />
In this view, click on the icon “WSDL Page” at the top right corner:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3406d1333510152-how-extract-info-sentence-click-wsdl-page-icon.png" border="0" alt="Name:  click wsdl page icon.png
Views: 23755
Size:  3.5 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Add a WSDL page</b></div><br />
    Select <i>WSDL Main</i> in the <i>Navigator</i> left pane. In the <i>Actions</i> pane on the right, type the URL: <i><a href="http://localhost:9898/HelloWeb?wsdl" target="_blank" rel="nofollow">http://localhost:9898/HelloWeb?wsdl</a></i> into the field <i>WSDL URL</i>, click <b>Go</b>.<br />
<br />
    The <i>WSDL Binding Details</i> page is displayed, which shows the <span style="font-family: Courier New"><font size="2">sayGreeting()</font></span> method as a web service method:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3407d1333510481-how-extract-info-sentence-wsdl-binding-details.png" border="0" alt="Name:  wsdl binding details.png
Views: 23823
Size:  36.4 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: WSDL Binding Details</b></div><br />
    Click on the <i>sayGreeting </i>name, the page Invoke a WSDL Operation is displayed, which shows details of the method <i>sayGreeting</i>:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3408d1333510582-how-extract-info-sentence-say-greeting-method-details.png" border="0" alt="Name:  say greeting method details.png
Views: 23786
Size:  35.2 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Invoke a WSDL Operation</b></div><br />
    The <span style="font-family: Courier New"><font size="2">sayGreeting()</font></span> method has one String argument, click on <b>Add link</b> to add value for the argument. Type “The President” into the text box:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3409d1333510746-how-extract-info-sentence-type-argument.png" border="0" alt="Name:  type argument.png
Views: 23673
Size:  4.1 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Set argument for web service method</b></div><br />
    Click <b>Go</b>, the web service method is invoked and return a response is returned in the <i>Status </i>bottom pane:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3410d1333510808-how-extract-info-sentence-method-return.png" border="0" alt="Name:  method return.png
Views: 23721
Size:  38.5 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Invoke the web service method</b></div><br />
That means the web service method is invoked successfully.<br />
<br />
<br />
<b><font size="3">Create a web service client</font></b><br />
<br />
So far we have been able to access the web service via a web interface inside Eclipse. It would be more interesting if the web service can be accessed via a standalone program. Such program is called a web service client, which we will write now. The steps to create a web service client are as follows:<br />
<ul><li style="">    First, we need to generate web service client Java sources file based on the WSDL page returned by the server. To do so, we should use the <span style="font-family: Courier New"><font size="2">wsimport</font></span> tool which comes with JDK. Syntax of the <span style="font-family: Courier New"><font size="2">wsimport</font></span> command line tool is:<br />
<br />
<div style="text-align: center;">wsimport [options] &lt;WSDL_URI&gt;</div><br />
<div style="margin-left:40px"><ul><li style="">        <span style="font-family: Courier New"><font size="2">&lt;WSDL_URI&gt;</font></span>: specifies URI of the web service’s WSDL page, it should be <i><a href="http://localhost:9898/HelloWeb?wsdl" target="_blank" rel="nofollow">http://localhost:9898/HelloWeb?wsdl</a></i> for our sample program.<br /></li><li style="">        <span style="font-family: Courier New"><font size="2">[options]</font></span>: specifies some options such as where to read web service source files and where to put generated source files. We will use these three options:</li></ul></div><br />
<div style="margin-left:40px"><div style="margin-left:40px"><ul><li style="">            -s: specifies the directory where to place generated source files.</li><li style="">            -p: specifies a package to place generated source files.</li><li style="">            -keep: keep the generated files.</li></ul></div></div><br />
So the command would be:<br />
<br />
<div style="text-align: center;"><span style="font-family: Courier New"><font size="2">wsimport -keep -s ./src -p com.mycompany.service.client <a href="http://localhost:9898/HelloWeb?wsdl" target="_blank" rel="nofollow">http://localhost:9898/HelloWeb?wsdl</a></font></span></div><br />
    We put the generated files into package <span style="font-family: Courier New"><font size="2">com.mycompany.service.client</font></span><br /></li><li style="">    Next, we create an Ant build file to execute the <span style="font-family: Courier New"><font size="2">wsimport</font></span> command within Eclipse IDE.<br />
<br />
<div style="margin-left:40px"><ul><li style="">        Create a new XML file, say <span style="font-family: Courier New"><font size="2">wsbuild.xml</font></span>, save it into project directory.</li><li style="">        Write content for the <span style="font-family: Courier New"><font size="2">wsbuild.xml</font></span> as follow:</li></ul></div><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">XML Code: Ant build script to generate web service client classes</div>

	<pre class="brush: xml">&lt;project name=&quot;My Web Service Build&quot; default=&quot;wsimport&quot;&gt;

    &lt;target name=&quot;wsimport&quot;&gt;

        &lt;exec executable=&quot;wsimport&quot;&gt;

            &lt;arg line=&quot;-keep -s ./src -p com.mycompany.service.client http://localhost:9898/HelloWeb?wsdl&quot;/&gt;

        &lt;/exec&gt;           

    &lt;/target&gt;

   

&lt;/project&gt;</pre>
	<script type="text/javascript">mh_sh_highlight_all('xml');</script>

</div></li><li style="">    Open Ant View by selecting <b>Window -&gt; Show View -&gt; Ant</b>.<br />
<div style="margin-left:40px"><br />
<ul><li style="">        Click on Add Buildfiles icon to add wsbuild.xml to the view.<br /></li><li style="">        Make sure the server program is still running, if not, start it. Click on Run icon to execute the default target of the Ant script.<br />
</li></ul></div><br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3411d1333511307-how-extract-info-sentence-ant-view.png" border="0" alt="Name:  ant view.png
Views: 23612
Size:  7.8 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Ant View</b></div><br />
The <span style="font-family: Courier New"><font size="2">wsimport</font></span> command will connect to the server and parse the WSDL page from the given URL to generate Java source files for web service client. And you should see a BUILD SUCCESSFUL message in the <i>Console </i>view.<br /></li><li style="">    Refresh the project in<i> Project Explorer</i> view, you will see there are 6 Java source files are generated under the package <span style="font-family: Courier New"><font size="2">com.mycompany.service.client</font></span>:<br />
<br />
<div style="text-align: center;"><img src="http://www.java-forums.org/attachments/xml/3412d1333511448-how-extract-info-sentence-generated-source-files.png" border="0" alt="Name:  generated source files.png
Views: 23616
Size:  12.2 KB" class="thumbnail" style="float:CONFIG" /><br />
<b>Figure: Generated Java source files</b></div></li><li style="">    The generated Java files are necessary for writing a web service client. We are interested in the class <span style="font-family: Courier New"><font size="2">HelloWebService.java</font></span> because it wraps all the details and exposes some methods ready for implementing a client.<br /></li><li style="">    Create a new Java class, say <span style="font-family: Courier New"><font size="2">Client.java</font></span> under the package <span style="font-family: Courier New"><font size="2">com.mycompany.service</font></span>, with a <span style="font-family: Courier New"><font size="2">main()</font></span> method. Update the source code of the class looks like this:</li></ul><br />
<br />
<div class="bbcode_container">
	<div class="bbcode_description">Java Code: The web service client code</div>

	<pre class="brush: java">package com.mycompany.service;

import com.mycompany.service.client.HelloWebService;

public class Client {

    public static void main(String&#91;&#93; args) {

        HelloWebService service = new HelloWebService();

        com.mycompany.service.client.HelloWeb helloWeb = service.getHelloWebPort();

        String response = helloWeb.sayGreeting(&quot;The President&quot;);

        System.out.println(&quot;Web service response: &quot; + response);

    }

}</pre>
	<script type="text/javascript">mh_sh_highlight_all('java');</script>

</div>
 And we are done writing a web service client. Be ready to test it now!<br />
<br />
<br />
<b><font size="3">Test the web service</font></b><br />
<br />
So far we have done both the server and the client for a sample web service. Both are standalone command line java programs and can be launched by selecting<b> Run -&gt; Run As -&gt; Java Application</b> from Eclipse’s main menu.<br />
<br />
<ul><li style="">    Start the server, and make sure you got the message “HelloWeb service is ready” in the console. The server is now waiting for request.</li><li style="">    Start the client, it will connect to the server, invoke the web service method and receive a response back. And finally you should see the message “Web service response: Greeting The President!”</li></ul><br />
<br />
<br />
In this article we only run the server and the client on a same computer. It is also possible to deploy the client and the server on two different machines.<br />
<br />
<ul><li style="">    The server part contains only two Java files: <span style="font-family: Courier New"><font size="2">HelloWeb.java</font></span> and <span style="font-family: Courier New"><font size="2">Server.java</font></span></li><li style="">    The client part contains all generated files under package <span style="font-family: Courier New"><font size="2">com.mycompany.service.client</font></span> and the <span style="font-family: Courier New"><font size="2">Client.java</font></span> file.</li></ul><br />
<br />
<br />
And remember to update the server address, port number and web service URI correctly.</blockquote>

]]></content:encoded>
			<dc:creator>Web Service</dc:creator>
			<guid isPermaLink="true">http://www.java-forums.org/blogs/web-service/1145-how-create-java-web-service.html</guid>
		</item>
	</channel>
</rss>
