Categories: SOAP

Creating a Simple Web Service and Clients with JAX-WS

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>This section shows how to build and deploy a simple web service and an application client&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;jaxws-simpleclientservice&period;gif" border&equals;"0" &sol;><&sol;div>&NewLine;<p>The starting point for developing a<i><b> JAX-WS <&sol;b><&sol;i>web service is a Java class annotated with the <i><b>javax&period;jws&period;WebService<&sol;b><&sol;i> annotation&period; The <i><b>&commat;WebService<&sol;b><&sol;i> annotation defines the class as a web service endpoint&period;<&sol;p>&NewLine;<p>A service endpoint interface or <b><i>service endpoint implementation &lpar;SEI&rpar;<&sol;i><&sol;b> is a Java interface or class&comma; respectively&comma; that declares the methods that a client can invoke on the service&period; An interface is not required when building a <i><b>JAX-WS<&sol;b><&sol;i> endpoint&period; The web service implementation class implicitly defines an <i><b>SEI<&sol;b><&sol;i>&period;<&sol;p>&NewLine;<p>You may specify an explicit interface by adding the <i><b>endpointInterface <&sol;b><&sol;i>element to the <i><b>&commat;WebService <&sol;b><&sol;i>annotation in the implementation class&period; You must then provide an interface that defines the public methods made available in the endpoint implementation class&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p>Here’s a guide to show you how to deploy <i><b>JAX-WS<&sol;b><&sol;i> web services on Tomcat servlet container&period; See following summary steps of a web service deployment&period;<&sol;p>&NewLine;<ol>&NewLine;<li>Create a web service<i><b>&lpar;WebService&rpar;<&sol;b><&sol;i>&period;<&sol;li>&NewLine;<li>Create a <i><b>sun-jaxws&period;xml<&sol;b><&sol;i>&comma; defines web service implementation class&period;<&sol;li>&NewLine;<li>Create a standard<b><i> web&period;xml<&sol;i><&sol;b>&comma; defines <i><b>WSServletContextListener<&sol;b><&sol;i>&comma; <i><b>WSServlet <&sol;b><&sol;i>and structure of a web project&period;<&sol;li>&NewLine;<li>Build tool to generate <i><b>WAR <&sol;b><&sol;i>file&period;<&sol;li>&NewLine;<li>Copy <i><b>JAX-WS<&sol;b><&sol;i> dependencies to &OpenCurlyDoubleQuote;<i><b>&dollar;&lbrace;Tomcat&rcub;&sol;lib<&sol;b><&sol;i>” folder&period;<&sol;li>&NewLine;<li>Copy WAR to &OpenCurlyDoubleQuote;<i><b>&dollar;&lbrace;Tomcat&rcub;&sol;webapp<&sol;b><&sol;i>” folder&period;<&sol;li>&NewLine;<li>Start It&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>Directory structure of this example&comma; so that you know where to put your files&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;directoryws&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>1&period; <i>WebServices-Create a Web Service Endpoint Interface<&sol;i><&sol;b><&sol;h2>&NewLine;<p>A simple<i><b> JAX-WS<&sol;b><&sol;i> hello world example&period;<br &sol;>&NewLine;<b>File &colon; <i>HelloWorld&period;java<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&semi; &NewLine; &NewLine;import javax&period;jws&period;WebMethod&semi; &NewLine;import javax&period;jws&period;WebService&semi; &NewLine;import javax&period;jws&period;soap&period;SOAPBinding&semi; &NewLine;import javax&period;jws&period;soap&period;SOAPBinding&period;Style&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; Service Endpoint Interface &NewLine; &ast;&sol; &NewLine; &NewLine;&commat;WebService &NewLine;&commat;SOAPBinding&lpar;style &equals; Style&period;RPC&rpar; &NewLine;public interface HelloWorld &lbrace; &NewLine; &commat;WebMethod &NewLine; String sayHelloWorld&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Create a Web Service Endpoint Implementation<&sol;b><&sol;h2>&NewLine;<p><i><b>File &colon; HelloWorldImpl&period;java<&sol;b><&sol;i><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&semi; &NewLine; &NewLine;import javax&period;jws&period;WebService&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; Service Implementation Bean &NewLine; &ast;&sol; &NewLine; &NewLine;&commat;WebService&lpar;endpointInterface &equals; "com&period;dineshonjava&period;ws&period;HelloWorld"&rpar; &NewLine;public class HelloWorldImpl implements HelloWorld &lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public String sayHelloWorld&lpar;&rpar; &lbrace; &NewLine; return "Hello World&excl;&excl; Dinesh on Java&excl;&excl;"&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Create a Endpoint Publisher-<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;endpoint&semi; &NewLine; &NewLine;import javax&period;xml&period;ws&period;Endpoint&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;ws&period;HelloWorldImpl&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; Endpoint publisher &NewLine; &ast;&sol; &NewLine;public class HelloWorldPublisher &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; Endpoint&period;publish&lpar;"http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello"&comma; new HelloWorldImpl&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><i><b>2&period; sun-jaxws&period;xml<&sol;b><&sol;i><&sol;h2>&NewLine;<p>Create a web service deployment descriptor&comma; which is also known as <i><b>JAX-WS RI<&sol;b><&sol;i> deployment descriptor – <i><b>sun-jaxws&period;xml&period;<&sol;b><&sol;i><br &sol;>&NewLine;<b>File &colon; sun-jaxws&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;endpoints xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;jax-ws&sol;ri&sol;runtime" version&equals;"2&period;0"&gt&semi; &NewLine; &lt&semi;endpoint name&equals;"HelloWorld" implementation&equals;"com&period;dineshonjava&period;ws&period;HelloWorldImpl" &NewLine; url-pattern&equals;"&sol;hello"&sol;&gt&semi; &NewLine;&lt&semi;&sol;endpoints&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>When user access<b><i> &sol;hello&sol;<&sol;i><&sol;b> URL path&comma; it will fire the declared web service&comma; which is <i><b>HelloWorldImpl&period;java<&sol;b><&sol;i>&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>3&period; <i>web&period;xml<&sol;i><&sol;b><&sol;h2>&NewLine;<p>Create a standard <i><b>web&period;xml<&sol;b><&sol;i> deployment descriptor for the deployment&period; Defines <i><b>WSServletContextListener<&sol;b><&sol;i> as listener class&comma; <i><b>WSServlet <&sol;b><&sol;i>as your hello <i><b>servlet<&sol;b><&sol;i>&period;<br &sol;>&NewLine;<b>File &colon; web&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;web-app xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xmlns&colon;web&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;2&lowbar;5&period;xsd" xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;3&lowbar;0&period;xsd" id&equals;"WebApp&lowbar;ID" version&equals;"3&period;0"&gt&semi; &NewLine; &lt&semi;display-name&gt&semi;WebService&lt&semi;&sol;display-name&gt&semi; &NewLine; &lt&semi;listener&gt&semi; &NewLine; &lt&semi;listener-class&gt&semi; &NewLine; com&period;sun&period;xml&period;ws&period;transport&period;http&period;servlet&period;WSServletContextListener &NewLine; &lt&semi;&sol;listener-class&gt&semi; &NewLine; &lt&semi;&sol;listener&gt&semi; &NewLine; &lt&semi;servlet&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;hello&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;servlet-class&gt&semi; &NewLine; com&period;sun&period;xml&period;ws&period;transport&period;http&period;servlet&period;WSServlet &NewLine; &lt&semi;&sol;servlet-class&gt&semi; &NewLine; &lt&semi;load-on-startup&gt&semi;1&lt&semi;&sol;load-on-startup&gt&semi; &NewLine; &lt&semi;&sol;servlet&gt&semi; &NewLine; &lt&semi;servlet-mapping&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;hello&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;url-pattern&gt&semi;&sol;hello&lt&semi;&sol;url-pattern&gt&semi; &NewLine; &lt&semi;&sol;servlet-mapping&gt&semi; &NewLine; &lt&semi;session-config&gt&semi; &NewLine; &lt&semi;session-timeout&gt&semi;120&lt&semi;&sol;session-timeout&gt&semi; &NewLine; &lt&semi;&sol;session-config&gt&semi; &NewLine; &lt&semi;&sol;web-app&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><i><b>4&period; WAR Content<&sol;b><&sol;i><&sol;h2>&NewLine;<p>Use Ant&comma; Maven or JAR command to build a WAR file to include everything inside&period; The WAR content should look like this &colon;<&sol;p>&NewLine;<p><i><b>WEB-INF&sol;classes&sol;com&sol;dineshonjava&sol;ws&sol;HelloWorld&period;class<&sol;b><&sol;i><br &sol;>&NewLine;<i><b>WEB-INF&sol;classes&sol;com&sol;dineshonjava&sol;ws&sol;HelloWorldImpl&period;class<&sol;b><&sol;i><br &sol;>&NewLine;<i><b>WEB-INF&sol;web&period;xml<&sol;b><&sol;i><br &sol;>&NewLine;<i><b>WEB-INF&sol;sun-jaxws&period;xml<&sol;b><&sol;i><br &sol;>&NewLine;OR<br &sol;>&NewLine;right click to project and export as war file with name <b><i>&&num;8220&semi;WebService&period;war<&sol;i><&sol;b>&&num;8221&semi; and deploy it to tomcat web server&period;<&sol;p>&NewLine;<h2><b>5&period; <i>JAX-WS <&sol;i>Dependencies<&sol;b><&sol;h2>&NewLine;<p>By default&comma; Tomcat does not comes with any <i><b>JAX-WS<&sol;b><&sol;i> dependencies&comma; So&comma; you have to include it manually&period;<br &sol;>&NewLine;<b>1&period; Go here<a href&equals;"http&colon;&sol;&sol;jax-ws&period;java&period;net&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener"><i> http&colon;&sol;&sol;jax-ws&period;java&period;net&sol;<&sol;i><&sol;a>&period;<&sol;b><br &sol;>&NewLine;<b>2&period; Download<i> JAX-WS RI<&sol;i> distribution&period;<&sol;b><br &sol;>&NewLine;<b>3&period; Unzip it and copy following <i>JAX-WS<&sol;i> dependencies to Tomcat library folder &OpenCurlyDoubleQuote;<i>&lbrace;&dollar;TOMCAT<&sol;i><i>&rcub;&sol;lib<&sol;i><&sol;b>&OpenCurlyDoubleQuote;&period;<br &sol;>&NewLine;<b><i>jaxb-impl&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>jaxws-api&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>jaxws-rt&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>gmbal-api-only&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>management-api&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>stax-ex&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>streambuffer&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>policy&period;jar<&sol;i><&sol;b><br &sol;>&NewLine;<b><i>ha-api&period;jar<&sol;i><&sol;b><&sol;p>&NewLine;<h2><i><b>6&period; Deployment<&sol;b><&sol;i><&sol;h2>&NewLine;<p>Copy the generated WAR file to <i><b>&lbrace;&dollar;TOMCAT&rcub;&sol;webapps&sol; <&sol;b><&sol;i>folder and start the Tomcat server&period;<br &sol;>&NewLine;For testing&comma; you can access this <b>URL <i>&colon; http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello<&sol;i><&sol;b>&comma; if you see following page&comma; it means web services are deploy successfully&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;webservice1-1024x307&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>you can access this URL <i>&colon; http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello&quest;wsdl<&sol;i>&comma; if you see following page<&sol;b><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><b><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;webservice2-1024x933&period;png" border&equals;"0" &sol;><&sol;b><&sol;div>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;wsdld&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Web Service Clients<&sol;b><&sol;h2>&NewLine;<p>Ok&comma; web service is deployed properly&comma; now let’s see how to create web service client to access to the published service&period;<&sol;p>&NewLine;<h2><b>1&period; Java Web Service Client<&sol;b><&sol;h2>&NewLine;<p><b>Without tool&comma; you can create a Java web service client like this &colon; <&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;client&semi; &NewLine; &NewLine;import java&period;net&period;MalformedURLException&semi; &NewLine;import java&period;net&period;URL&semi; &NewLine; &NewLine;import javax&period;xml&period;namespace&period;QName&semi; &NewLine;import javax&period;xml&period;ws&period;Service&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;ws&period;HelloWorld&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class HelloWorldClient &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; URL url&semi; &NewLine; try &lbrace; &NewLine; url &equals; new URL&lpar;"http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello&quest;wsdl"&rpar;&semi; &NewLine; &NewLine; &sol;&sol;1st argument service URI&comma; refer to wsdl document above &NewLine; &sol;&sol;2nd argument is service name&comma; refer to wsdl document above &NewLine; QName qname &equals; new QName&lpar;"http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&comma; "HelloWorldImplService"&rpar;&semi; &NewLine; &NewLine; Service service &equals; Service&period;create&lpar;url&comma; qname&rpar;&semi; &NewLine; &NewLine; HelloWorld hello &equals; service&period;getPort&lpar;HelloWorld&period;class&rpar;&semi; &NewLine; &NewLine; System&period;out&period;println&lpar;hello&period;sayHelloWorld&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; catch &lpar;MalformedURLException e&rpar; &lbrace; &NewLine; e&period;printStackTrace&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;outws&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b><br &sol;>&NewLine;2&period; Java Web Service Client via <i>wsimport <&sol;i>tool<br &sol;>&NewLine;<&sol;b><&sol;h2>&NewLine;<p><b>Alternative&comma; you can use &OpenCurlyDoubleQuote;<i>wsimport<&sol;i>” tool to parse the published wsdl file&comma; and generate necessary client files &lpar;stub&rpar; to access the published web service&period;<&sol;b><br &sol;>&NewLine;<b><i><b>Where is wsimport&quest;<&sol;b><&sol;i><br &sol;>&NewLine;This <i><b>wsimport<&sol;b> <&sol;i>tool is bundle with the <i>JDK<&sol;i>&comma; you can find it at &OpenCurlyDoubleQuote;<i>JDK&lowbar;PATH&sol;bin<&sol;i>” folder&period; <&sol;b><br &sol;>&NewLine;<i><b>Use the following command for parsing the WSDL file&&num;8230&semi;<&sol;b><&sol;i><&sol;p>&NewLine;<p><i><b> wsimport -keep http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello&quest;wsdl<&sol;b><&sol;i><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;wsimport&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>It will generate necessary client files&comma; which is depends on the provided <i><b>wsdl <&sol;b><&sol;i>file&period; In this case&comma; it will generate one interface and one service implementation file&period;<br &sol;>&NewLine;<b>1&period; <i>HelloWorld&period;java<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&semi; &NewLine; &NewLine;import javax&period;jws&period;WebMethod&semi; &NewLine;import javax&period;jws&period;WebResult&semi; &NewLine;import javax&period;jws&period;WebService&semi; &NewLine;import javax&period;jws&period;soap&period;SOAPBinding&semi; &NewLine;import javax&period;xml&period;ws&period;Action&semi; &NewLine; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; This class was generated by the JAX-WS RI&period; &NewLine; &ast; JAX-WS RI 2&period;2&period;2 in JDK 7 &NewLine; &ast; Generated source version&colon; 2&period;2 &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;WebService&lpar;name &equals; "HelloWorld"&comma; targetNamespace &equals; "http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&rpar; &NewLine;&commat;SOAPBinding&lpar;style &equals; SOAPBinding&period;Style&period;RPC&rpar; &NewLine;public interface HelloWorld &lbrace; &NewLine; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast; &commat;return &NewLine; &ast; returns java&period;lang&period;String &NewLine; &ast;&sol; &NewLine; &commat;WebMethod &NewLine; &commat;WebResult&lpar;partName &equals; "return"&rpar; &NewLine; &commat;Action&lpar;input &equals; "http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;HelloWorld&sol;sayHelloWorldRequest"&comma; output &equals; "http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;HelloWorld&sol;sayHelloWorldResponse"&rpar; &NewLine; public String sayHelloWorld&lpar;&rpar;&semi; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>2&period; <i>HelloWorldImplService&period;java<&sol;i><&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;ws&semi; &NewLine; &NewLine;import java&period;net&period;MalformedURLException&semi; &NewLine;import java&period;net&period;URL&semi; &NewLine;import javax&period;xml&period;namespace&period;QName&semi; &NewLine;import javax&period;xml&period;ws&period;Service&semi; &NewLine;import javax&period;xml&period;ws&period;WebEndpoint&semi; &NewLine;import javax&period;xml&period;ws&period;WebServiceClient&semi; &NewLine;import javax&period;xml&period;ws&period;WebServiceException&semi; &NewLine;import javax&period;xml&period;ws&period;WebServiceFeature&semi; &NewLine; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; This class was generated by the JAX-WS RI&period; &NewLine; &ast; JAX-WS RI 2&period;2&period;2 in JDK 7 &NewLine; &ast; Generated source version&colon; 2&period;2 &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;WebServiceClient&lpar;name &equals; "HelloWorldImplService"&comma; targetNamespace &equals; "http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&comma; wsdlLocation &equals; "http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello&quest;wsdl"&rpar; &NewLine;public class HelloWorldImplService &NewLine; extends Service &NewLine;&lbrace; &NewLine; &NewLine; private final static URL HELLOWORLDIMPLSERVICE&lowbar;WSDL&lowbar;LOCATION&semi; &NewLine; private final static WebServiceException HELLOWORLDIMPLSERVICE&lowbar;EXCEPTION&semi; &NewLine; private final static QName HELLOWORLDIMPLSERVICE&lowbar;QNAME &equals; new QName&lpar;"http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&comma; "HelloWorldImplService"&rpar;&semi; &NewLine; &NewLine; static &lbrace; &NewLine; URL url &equals; null&semi; &NewLine; WebServiceException e &equals; null&semi; &NewLine; try &lbrace; &NewLine; url &equals; new URL&lpar;"http&colon;&sol;&sol;localhost&colon;8181&sol;sdnext&sol;hello&quest;wsdl"&rpar;&semi; &NewLine; &rcub; catch &lpar;MalformedURLException ex&rpar; &lbrace; &NewLine; e &equals; new WebServiceException&lpar;ex&rpar;&semi; &NewLine; &rcub; &NewLine; HELLOWORLDIMPLSERVICE&lowbar;WSDL&lowbar;LOCATION &equals; url&semi; &NewLine; HELLOWORLDIMPLSERVICE&lowbar;EXCEPTION &equals; e&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;&rpar; &lbrace; &NewLine; super&lpar;&lowbar;&lowbar;getWsdlLocation&lpar;&rpar;&comma; HELLOWORLDIMPLSERVICE&lowbar;QNAME&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;WebServiceFeature&period;&period;&period; features&rpar; &lbrace; &NewLine; super&lpar;&lowbar;&lowbar;getWsdlLocation&lpar;&rpar;&comma; HELLOWORLDIMPLSERVICE&lowbar;QNAME&comma; features&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;URL wsdlLocation&rpar; &lbrace; &NewLine; super&lpar;wsdlLocation&comma; HELLOWORLDIMPLSERVICE&lowbar;QNAME&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;URL wsdlLocation&comma; WebServiceFeature&period;&period;&period; features&rpar; &lbrace; &NewLine; super&lpar;wsdlLocation&comma; HELLOWORLDIMPLSERVICE&lowbar;QNAME&comma; features&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;URL wsdlLocation&comma; QName serviceName&rpar; &lbrace; &NewLine; super&lpar;wsdlLocation&comma; serviceName&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; public HelloWorldImplService&lpar;URL wsdlLocation&comma; QName serviceName&comma; WebServiceFeature&period;&period;&period; features&rpar; &lbrace; &NewLine; super&lpar;wsdlLocation&comma; serviceName&comma; features&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast; &commat;return &NewLine; &ast; returns HelloWorld &NewLine; &ast;&sol; &NewLine; &commat;WebEndpoint&lpar;name &equals; "HelloWorldImplPort"&rpar; &NewLine; public HelloWorld getHelloWorldImplPort&lpar;&rpar; &lbrace; &NewLine; return super&period;getPort&lpar;new QName&lpar;"http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&comma; "HelloWorldImplPort"&rpar;&comma; HelloWorld&period;class&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast; &commat;param features &NewLine; &ast; A list of &lbrace;&commat;link javax&period;xml&period;ws&period;WebServiceFeature&rcub; to configure on the proxy&period; Supported features not in the features parameter will have their default values&period; &NewLine; &ast; &commat;return &NewLine; &ast; returns HelloWorld &NewLine; &ast;&sol; &NewLine; &commat;WebEndpoint&lpar;name &equals; "HelloWorldImplPort"&rpar; &NewLine; public HelloWorld getHelloWorldImplPort&lpar;WebServiceFeature&period;&period;&period; features&rpar; &lbrace; &NewLine; return super&period;getPort&lpar;new QName&lpar;"http&colon;&sol;&sol;ws&period;dineshonjava&period;com&sol;"&comma; "HelloWorldImplPort"&rpar;&comma; HelloWorld&period;class&comma; features&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; private static URL &lowbar;&lowbar;getWsdlLocation&lpar;&rpar; &lbrace; &NewLine; if &lpar;HELLOWORLDIMPLSERVICE&lowbar;EXCEPTION&excl;&equals; null&rpar; &lbrace; &NewLine; throw HELLOWORLDIMPLSERVICE&lowbar;EXCEPTION&semi; &NewLine; &rcub; &NewLine; return HELLOWORLDIMPLSERVICE&lowbar;WSDL&lowbar;LOCATION&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Now&comma; create a Java web service client which depends on the above generated files&period;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;client&semi; &NewLine; &NewLine;import com&period;dineshonjava&period;ws&period;HelloWorld&semi; &NewLine;import com&period;dineshonjava&period;ws&period;HelloWorldImplService&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class HelloWorldClient &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; HelloWorldImplService helloService &equals; new HelloWorldImplService&lpar;&rpar;&semi; &NewLine; HelloWorld hello &equals; helloService&period;getHelloWorldImplPort&lpar;&rpar;&semi; &NewLine; &NewLine; System&period;out&period;println&lpar;hello&period;sayHelloWorld&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Here’s the output<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;05&sol;outws&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b>Download SourceCode<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;WebServices&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">WebService&period;zip<&sol;a><&sol;b><&sol;h2>&NewLine;<p><i><b>References<&sol;b><&sol;i><br &sol;>&NewLine;<i><b><a href&equals;"http&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Web&lowbar;service" target&equals;"&lowbar;blank" rel&equals;"noopener">Wikipedia for Web Service<&sol;a><&sol;b><&sol;i><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-ws-hello-world-example&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;why-contract-first-in-web-service&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;what-is-soap&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '450'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago