<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>This section shows how to build and deploy a simple web service and an application client.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/jaxws-simpleclientservice.gif" border="0" /></div>
<p>The starting point for developing a<i><b> JAX-WS </b></i>web service is a Java class annotated with the <i><b>javax.jws.WebService</b></i> annotation. The <i><b>@WebService</b></i> annotation defines the class as a web service endpoint.</p>
<p>A service endpoint interface or <b><i>service endpoint implementation (SEI)</i></b> is a Java interface or class, respectively, that declares the methods that a client can invoke on the service. An interface is not required when building a <i><b>JAX-WS</b></i> endpoint. The web service implementation class implicitly defines an <i><b>SEI</b></i>.</p>
<p>You may specify an explicit interface by adding the <i><b>endpointInterface </b></i>element to the <i><b>@WebService </b></i>annotation in the implementation class. You must then provide an interface that defines the public methods made available in the endpoint implementation class.</p>
<div id="ads-id" align="center"></div>
<p>Here’s a guide to show you how to deploy <i><b>JAX-WS</b></i> web services on Tomcat servlet container. See following summary steps of a web service deployment.</p>
<ol>
<li>Create a web service<i><b>(WebService)</b></i>.</li>
<li>Create a <i><b>sun-jaxws.xml</b></i>, defines web service implementation class.</li>
<li>Create a standard<b><i> web.xml</i></b>, defines <i><b>WSServletContextListener</b></i>, <i><b>WSServlet </b></i>and structure of a web project.</li>
<li>Build tool to generate <i><b>WAR </b></i>file.</li>
<li>Copy <i><b>JAX-WS</b></i> dependencies to “<i><b>${Tomcat}/lib</b></i>” folder.</li>
<li>Copy WAR to “<i><b>${Tomcat}/webapp</b></i>” folder.</li>
<li>Start It.</li>
</ol>
<p>Directory structure of this example, so that you know where to put your files.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/directoryws.png" border="0" /></div>
<h2><b>1. <i>WebServices-Create a Web Service Endpoint Interface</i></b></h2>
<p>A simple<i><b> JAX-WS</b></i> hello world example.<br />
<b>File : <i>HelloWorld.java</i></b></p>
<pre class="highlight">package com.dineshonjava.ws; 
 
import javax.jws.WebMethod; 
import javax.jws.WebService; 
import javax.jws.soap.SOAPBinding; 
import javax.jws.soap.SOAPBinding.Style; 
 
/** 
 * @author Dinesh Rajput 
 * Service Endpoint Interface 
 */ 
 
@WebService 
@SOAPBinding(style = Style.RPC) 
public interface HelloWorld { 
 @WebMethod 
 String sayHelloWorld(); 
} 
</pre>
<h2><b>Create a Web Service Endpoint Implementation</b></h2>
<p><i><b>File : HelloWorldImpl.java</b></i></p>
<pre class="highlight">package com.dineshonjava.ws; 
 
import javax.jws.WebService; 
 
/** 
 * @author Dinesh Rajput 
 * Service Implementation Bean 
 */ 
 
@WebService(endpointInterface = "com.dineshonjava.ws.HelloWorld") 
public class HelloWorldImpl implements HelloWorld { 
 
 @Override 
 public String sayHelloWorld() { 
 return "Hello World!! Dinesh on Java!!"; 
 } 
} 
</pre>
<h2><b>Create a Endpoint Publisher-</b></h2>
<pre class="highlight">package com.dineshonjava.endpoint; 
 
import javax.xml.ws.Endpoint; 
 
import com.dineshonjava.ws.HelloWorldImpl; 
 
/** 
 * @author Dinesh Rajput 
 * Endpoint publisher 
 */ 
public class HelloWorldPublisher { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 Endpoint.publish("http://localhost:8181/sdnext/hello", new HelloWorldImpl()); 
 } 
 
} 
</pre>
<h2><i><b>2. sun-jaxws.xml</b></i></h2>
<p>Create a web service deployment descriptor, which is also known as <i><b>JAX-WS RI</b></i> deployment descriptor – <i><b>sun-jaxws.xml.</b></i><br />
<b>File : sun-jaxws.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">; 
 <;endpoint name="HelloWorld" implementation="com.dineshonjava.ws.HelloWorldImpl" 
 url-pattern="/hello"/>; 
<;/endpoints>; 
</pre>
<p>When user access<b><i> /hello/</i></b> URL path, it will fire the declared web service, which is <i><b>HelloWorldImpl.java</b></i>.<br />
<b></b></p>
<h2><b>3. <i>web.xml</i></b></h2>
<p>Create a standard <i><b>web.xml</b></i> deployment descriptor for the deployment. Defines <i><b>WSServletContextListener</b></i> as listener class, <i><b>WSServlet </b></i>as your hello <i><b>servlet</b></i>.<br />
<b>File : web.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;WebService<;/display-name>; 
 <;listener>; 
 <;listener-class>; 
 com.sun.xml.ws.transport.http.servlet.WSServletContextListener 
 <;/listener-class>; 
 <;/listener>; 
 <;servlet>; 
 <;servlet-name>;hello<;/servlet-name>; 
 <;servlet-class>; 
 com.sun.xml.ws.transport.http.servlet.WSServlet 
 <;/servlet-class>; 
 <;load-on-startup>;1<;/load-on-startup>; 
 <;/servlet>; 
 <;servlet-mapping>; 
 <;servlet-name>;hello<;/servlet-name>; 
 <;url-pattern>;/hello<;/url-pattern>; 
 <;/servlet-mapping>; 
 <;session-config>; 
 <;session-timeout>;120<;/session-timeout>; 
 <;/session-config>; 
 <;/web-app>; 
</pre>
<h2><i><b>4. WAR Content</b></i></h2>
<p>Use Ant, Maven or JAR command to build a WAR file to include everything inside. The WAR content should look like this :</p>
<p><i><b>WEB-INF/classes/com/dineshonjava/ws/HelloWorld.class</b></i><br />
<i><b>WEB-INF/classes/com/dineshonjava/ws/HelloWorldImpl.class</b></i><br />
<i><b>WEB-INF/web.xml</b></i><br />
<i><b>WEB-INF/sun-jaxws.xml</b></i><br />
OR<br />
right click to project and export as war file with name <b><i>&#8220;WebService.war</i></b>&#8221; and deploy it to tomcat web server.</p>
<h2><b>5. <i>JAX-WS </i>Dependencies</b></h2>
<p>By default, Tomcat does not comes with any <i><b>JAX-WS</b></i> dependencies, So, you have to include it manually.<br />
<b>1. Go here<a href="http://jax-ws.java.net/" target="_blank" rel="noopener"><i> http://jax-ws.java.net/</i></a>.</b><br />
<b>2. Download<i> JAX-WS RI</i> distribution.</b><br />
<b>3. Unzip it and copy following <i>JAX-WS</i> dependencies to Tomcat library folder “<i>{$TOMCAT</i><i>}/lib</i></b>“.<br />
<b><i>jaxb-impl.jar</i></b><br />
<b><i>jaxws-api.jar</i></b><br />
<b><i>jaxws-rt.jar</i></b><br />
<b><i>gmbal-api-only.jar</i></b><br />
<b><i>management-api.jar</i></b><br />
<b><i>stax-ex.jar</i></b><br />
<b><i>streambuffer.jar</i></b><br />
<b><i>policy.jar</i></b><br />
<b><i>ha-api.jar</i></b></p>
<h2><i><b>6. Deployment</b></i></h2>
<p>Copy the generated WAR file to <i><b>{$TOMCAT}/webapps/ </b></i>folder and start the Tomcat server.<br />
For testing, you can access this <b>URL <i>: http://localhost:8181/sdnext/hello</i></b>, if you see following page, it means web services are deploy successfully.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/webservice1-1024x307.png" border="0" /></div>
<p><b>you can access this URL <i>: http://localhost:8181/sdnext/hello?wsdl</i>, if you see following page</b></p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><b><img src="https://dineshonjava.com/wp-content/uploads/2013/05/webservice2-1024x933.png" border="0" /></b></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/wsdld.png" border="0" /></div>
<h2><b>Web Service Clients</b></h2>
<p>Ok, web service is deployed properly, now let’s see how to create web service client to access to the published service.</p>
<h2><b>1. Java Web Service Client</b></h2>
<p><b>Without tool, you can create a Java web service client like this : </b></p>
<pre class="highlight">package com.dineshonjava.client; 
 
import java.net.MalformedURLException; 
import java.net.URL; 
 
import javax.xml.namespace.QName; 
import javax.xml.ws.Service; 
 
import com.dineshonjava.ws.HelloWorld; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class HelloWorldClient { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 URL url; 
 try { 
 url = new URL("http://localhost:8181/sdnext/hello?wsdl"); 
 
 //1st argument service URI, refer to wsdl document above 
 //2nd argument is service name, refer to wsdl document above 
 QName qname = new QName("http://ws.dineshonjava.com/", "HelloWorldImplService"); 
 
 Service service = Service.create(url, qname); 
 
 HelloWorld hello = service.getPort(HelloWorld.class); 
 
 System.out.println(hello.sayHelloWorld()); 
 } catch (MalformedURLException e) { 
 e.printStackTrace(); 
 } 
 } 
 
} 
</pre>
<p><b>Output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/outws.png" border="0" /></div>
<h2><b><br />
2. Java Web Service Client via <i>wsimport </i>tool<br />
</b></h2>
<p><b>Alternative, you can use “<i>wsimport</i>” tool to parse the published wsdl file, and generate necessary client files (stub) to access the published web service.</b><br />
<b><i><b>Where is wsimport?</b></i><br />
This <i><b>wsimport</b> </i>tool is bundle with the <i>JDK</i>, you can find it at “<i>JDK_PATH/bin</i>” folder. </b><br />
<i><b>Use the following command for parsing the WSDL file&#8230;</b></i></p>
<p><i><b> wsimport -keep http://localhost:8181/sdnext/hello?wsdl</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/wsimport.png" border="0" /></div>
<p>It will generate necessary client files, which is depends on the provided <i><b>wsdl </b></i>file. In this case, it will generate one interface and one service implementation file.<br />
<b>1. <i>HelloWorld.java</i></b></p>
<pre class="highlight">package com.dineshonjava.ws; 
 
import javax.jws.WebMethod; 
import javax.jws.WebResult; 
import javax.jws.WebService; 
import javax.jws.soap.SOAPBinding; 
import javax.xml.ws.Action; 
 
 
/** 
 * This class was generated by the JAX-WS RI. 
 * JAX-WS RI 2.2.2 in JDK 7 
 * Generated source version: 2.2 
 * 
 */ 
@WebService(name = "HelloWorld", targetNamespace = "http://ws.dineshonjava.com/") 
@SOAPBinding(style = SOAPBinding.Style.RPC) 
public interface HelloWorld { 
 
 
 /** 
 * 
 * @return 
 * returns java.lang.String 
 */ 
 @WebMethod 
 @WebResult(partName = "return") 
 @Action(input = "http://ws.dineshonjava.com/HelloWorld/sayHelloWorldRequest", output = "http://ws.dineshonjava.com/HelloWorld/sayHelloWorldResponse") 
 public String sayHelloWorld(); 
 
} 
</pre>
<p><b>2. <i>HelloWorldImplService.java</i></b></p>
<pre class="highlight">package com.dineshonjava.ws; 
 
import java.net.MalformedURLException; 
import java.net.URL; 
import javax.xml.namespace.QName; 
import javax.xml.ws.Service; 
import javax.xml.ws.WebEndpoint; 
import javax.xml.ws.WebServiceClient; 
import javax.xml.ws.WebServiceException; 
import javax.xml.ws.WebServiceFeature; 
 
 
/** 
 * This class was generated by the JAX-WS RI. 
 * JAX-WS RI 2.2.2 in JDK 7 
 * Generated source version: 2.2 
 * 
 */ 
@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.dineshonjava.com/", wsdlLocation = "http://localhost:8181/sdnext/hello?wsdl") 
public class HelloWorldImplService 
 extends Service 
{ 
 
 private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION; 
 private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION; 
 private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.dineshonjava.com/", "HelloWorldImplService"); 
 
 static { 
 URL url = null; 
 WebServiceException e = null; 
 try { 
 url = new URL("http://localhost:8181/sdnext/hello?wsdl"); 
 } catch (MalformedURLException ex) { 
 e = new WebServiceException(ex); 
 } 
 HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url; 
 HELLOWORLDIMPLSERVICE_EXCEPTION = e; 
 } 
 
 public HelloWorldImplService() { 
 super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME); 
 } 
 
 public HelloWorldImplService(WebServiceFeature... features) { 
 super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features); 
 } 
 
 public HelloWorldImplService(URL wsdlLocation) { 
 super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME); 
 } 
 
 public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) { 
 super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features); 
 } 
 
 public HelloWorldImplService(URL wsdlLocation, QName serviceName) { 
 super(wsdlLocation, serviceName); 
 } 
 
 public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) { 
 super(wsdlLocation, serviceName, features); 
 } 
 
 /** 
 * 
 * @return 
 * returns HelloWorld 
 */ 
 @WebEndpoint(name = "HelloWorldImplPort") 
 public HelloWorld getHelloWorldImplPort() { 
 return super.getPort(new QName("http://ws.dineshonjava.com/", "HelloWorldImplPort"), HelloWorld.class); 
 } 
 
 /** 
 * 
 * @param features 
 * A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the features parameter will have their default values. 
 * @return 
 * returns HelloWorld 
 */ 
 @WebEndpoint(name = "HelloWorldImplPort") 
 public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) { 
 return super.getPort(new QName("http://ws.dineshonjava.com/", "HelloWorldImplPort"), HelloWorld.class, features); 
 } 
 
 private static URL __getWsdlLocation() { 
 if (HELLOWORLDIMPLSERVICE_EXCEPTION!= null) { 
 throw HELLOWORLDIMPLSERVICE_EXCEPTION; 
 } 
 return HELLOWORLDIMPLSERVICE_WSDL_LOCATION; 
 } 
 
} 
</pre>
<p><b>Now, create a Java web service client which depends on the above generated files.</b></p>
<pre class="highlight">package com.dineshonjava.client; 
 
import com.dineshonjava.ws.HelloWorld; 
import com.dineshonjava.ws.HelloWorldImplService; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class HelloWorldClient { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 HelloWorldImplService helloService = new HelloWorldImplService(); 
 HelloWorld hello = helloService.getHelloWorldImplPort(); 
 
 System.out.println(hello.sayHelloWorld()); 
 } 
 
} 
</pre>
<p><b>Here’s the output</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/outws.png" border="0" /></div>
<h2><b>Download SourceCode</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/WebServices.zip?attredirects=0&;d=1" target="_blank" rel="noopener">WebService.zip</a></b></h2>
<p><i><b>References</b></i><br />
<i><b><a href="http://en.wikipedia.org/wiki/Web_service" target="_blank" rel="noopener">Wikipedia for Web Service</a></b></i></p>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/jax-ws-hello-world-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/why-contract-first-in-web-service/">Next</a> >;>;</b></div>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/what-is-soap/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '450'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…