JAX Web Services end to end examples (Server & Client code, Tomcat & Eclipse)


Requirements:
JDK 1.6 or 1.6+
Eclipse
Tomcat 6

JAX-WS-JAVA-WEB-SERVICES













Let’s start one by one step to achieve it:


Step : Create a Dynamic Web project in Eclipse
Open the eclipse by double click on the eclipse – Shortcut
File > New > Dynamic Web project
Project Name: jaxwsprojectineclipse
Target runtime: Apache Tomcat v6.0
Dynamic web module version: 2.5
Click Next
Click Next
Select or check: Generate web.xml deployment descriptor
Click on the Finish.

Step : Create the GreetingWebService.java service interface
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select Interface
Name : GreetingWebService
Package : com.tutorialbyexample.webservicejaxws
Click on the Finish

Copy and pest below code:

GreetingWebService.java

package com.tutorialbyexample.webservicejaxws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

/**
* @author Vinod Kumar Web Service endpoint interface
*/

@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface GreetingWebService {

                /**
                 * sayWelcome with parameter as string
                 */
                @WebMethod
                String sayWelcome(@WebParam(name = "sName") String sName);
}

Step : Create the GreetingWebServiceImpl.java service interface
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select class
Name : GreetingWebServiceImpl
Package : com.tutorialbyexample.webservicejaxws
Click on the Finish

Copy and pest below code:

GreetingWebServiceImpl.java

package com.tutorialbyexample.webservicejaxws;

import javax.jws.WebService;

import com.tutorialbyexample.webservicejaxws.GreetingWebService;

/**
*
 * @author Vinod Kumar This is service implementation class
*/

@WebService(endpointInterface = "com.tutorialbyexample.webservicejaxws.GreetingWebService")
public class GreetingWebServiceImpl implements GreetingWebService {

                @Override
                public String sayWelcome(String sName) {
                                return "Welcome " + sName + " !";
                }
}

Step : Add/update the deployment descriptor sun-jaxws.xml and web.xml
Right click on the WebContent folder located under jaxwsprojectineclipse
Select New
Others
Type XML
Select XML File
Next
Type the file name: sun-jaxws.xml
Click on the Finish

Copy and pest sun-jaxws.xml:
<?xml version="1.0"encoding="UTF-8"?>
       version="2.0">
       <endpoint name="GreetingWebService"
              implementation="com.tutorialbyexample.webservicejaxws.GreetingWebServiceImpl"
              url-pattern="/GreetingWebService"/>
</endpoints>

Update/Add the require line in web.xml

<?xml version="1.0"encoding="UTF-8"?>
  <display-name>jaxwsprojectineclipse</display-name>
  <listener>
    <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
  </listener>
  <servlet>
    <servlet-name>GreetingWebService</servlet-name>
    <servlet-class>
              com.sun.xml.ws.transport.http.servlet.WSServlet
        </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GreetingWebService</servlet-name>
    <url-pattern>/GreetingWebService</url-pattern>
  </servlet-mapping>
</web-app>

Step : Write the WebServicesClient.jav client code
Right click on the src folder located under jaxwsprojectineclipse > Java Resource
Select New
Select class
Name : WebServicesClient
Package : com.tutorialbyexample.webservicejaxws.client
Click on the Finish

Copy and pest below code:

WebServicesClient.java
package com.tutorialbyexample.webservicejaxws.client;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.tutorialbyexample.webservicejaxws.GreetingWebService;

/**
*
 * @author Vinod Kumar This is service implementation class
*/

public class WebServicesClient {

                public static void main(String[] args) throws MalformedURLException {
                                URL url = new URL(
                                                                "http://localhost:8080/jaxwsprojectineclipse/GreetingWebService?wsdl");

                                // argument first, refer from wsdl
                                // argument second, refer from wsdl
                                QName qname = new QName(
                                                                "http://webservicejaxws.tutorialbyexample.com/",
                                                                "GreetingWebServiceImplService");

                                Service service = Service.create(url, qname);

                                GreetingWebService hello = service.getPort(GreetingWebService.class);
                                System.out.println(hello.sayWelcome("Vinod Kumar"));
                }
}


Step : Write the client as jsp file WSClientJSP.jsp
Right click on the WebContent folder located under jaxwsprojectineclipse
Select New
Others
Type JSP
Select JSP file
Next
Type the file name: WSClientJSP.jsp
Click on the Finish

Copy and pest below code WSClientJSP.jsp:
<%@ pagelanguage="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<%@ page
       import="java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service,com.tutorialbyexample.webservicejaxws.GreetingWebService"%>
<!DOCTYPE htmlPUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

       <%
              URL url = new URL(
                           "http://localhost:8080/jaxwsprojectineclipse/GreetingWebService?wsdl");

              // argument first, refer from wsdl
              // argument second, refer from wsdl
              QName qname = new QName("http://webservicejaxws.tutorialbyexample.com/",
                           "GreetingWebServiceImplService");

              Service service = Service.create(url, qname);

              GreetingWebService hello = service
                           .getPort(GreetingWebService.class);
              out.println(hello.sayWelcome("Vinod Kumar"));
       %>
</body>
</html>


Step: Deploy war file jaxwsprojectineclipse .war in Tomcat
Right click on the jaxwsprojectineclipse
Select Export
Select WAR File
Let it be Web Project Name : jaxwsprojectineclipse
Select or Type the Destination: C:\Tomcate\apache-tomcat-6.0.39\webapps\jaxwsprojectineclipse.war
Note: This is the location of current tomcat installation directory.
Select or check
Export Source files
Overwrite existing file
Click the Finish
jaxwsprojectineclipse.war war file will create in Tomcat C:\Tomcate\apache-tomcat-6.0.39\webapps\

Step: Start the Tomcat
Open the Tomcat installation directory C:\Tomcate\apache-tomcat-6.0.39\bin
Double click on the startup.bat file

Tomcat will start:
INFO: Starting Coyote HTTP/1.1 on http-8080
org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/13  config=null
org.apache.catalina.startup.Catalina start
INFO: Server startup in 1441 ms

Step: Verify the deploy web services by WSDL file
Open the any web browser and type the end point url

Output:
  <?xml version="1.0" encoding="UTF-8" ?>
- <!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.10 svn-revision#919b322c92f13ad085a933e8dd6dd35d4947364b. 
  -->
- <!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.10 svn-revision#919b322c92f13ad085a933e8dd6dd35d4947364b. 
  -->
- <types>
- <xsd:schema>
  </xsd:schema>
  </types>
- <message name="sayWelcome">
  <partname="parameters" element="tns:sayWelcome" />
  </message>
- <message name="sayWelcomeResponse">
  <partname="parameters" element="tns:sayWelcomeResponse" />
  </message>
- <portType name="GreetingWebService">
- <operation name="sayWelcome">
  <outputwsam:Action="http://webservicejaxws.tutorialbyexample.com/GreetingWebService/sayWelcomeResponse" message="tns:sayWelcomeResponse" />
  </operation>
  </portType>
- <binding name="GreetingWebServiceImplPortBinding" type="tns:GreetingWebService">
  <soap:bindingtransport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <operation name="sayWelcome">
  <soap:operationsoapAction="" />
- <input>
  <soap:bodyuse="literal" />
  </input>
- <output>
  <soap:bodyuse="literal" />
  </output>
  </operation>
  </binding>
- <service name="GreetingWebServiceImplService">
- <port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
  </port>
  </service>
  </definitions>

Step: Verify the deploy services with WSClientJSP.jsp

Output:
Welcome Vinod Kumar !

Step: Verify the deploy web services by DII client
Right click on the WebServicesClient.java file located under jaxwsprojectineclipse > Java Resource
Select Run As
Select Java Application
Output will there in Console
com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
INFO: Dynamically creating request wrapper Class com.tutorialbyexample.webservicejaxws.jaxws.SayWelcome
com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
INFO: Dynamically creating response wrapper bean Class com.tutorialbyexample.webservicejaxws.jaxws.SayWelcomeResponse
Welcome Vinod Kumar !

Please like and share it!!!

3 comments:

  1. explained it...nice..server side and client code....for webservices...

    ReplyDelete
  2. Online Microservices and docker training https://cloudnixiass.com/#courses

    ReplyDelete