Requirements:
JDK 1.6 or 1.6+
Eclipse
Tomcat 6
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"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"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>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(
// argument first, refer from wsdl
// argument second, refer from wsdl
QName qname = new QName(
"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(
// argument first, refer from wsdl
// argument second, refer from wsdl
"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.
-->
- <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservicejaxws.tutorialbyexample.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservicejaxws.tutorialbyexample.com/" name="GreetingWebServiceImplService">
- <types>
- <xsd:schema>
<xsd:importnamespace="http://webservicejaxws.tutorialbyexample.com/" schemaLocation="http://localhost:8080/jaxwsprojectineclipse/GreetingWebService?xsd=1" />
</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">
<inputwsam:Action="http://webservicejaxws.tutorialbyexample.com/GreetingWebService/sayWelcomeRequest" message="tns: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">
<soap:addresslocation="http://localhost:8080/jaxwsprojectineclipse/GreetingWebService" />
</port>
</service>
</definitions>
Step: Verify the deploy services with WSClientJSP.jsp
Open the any browser and type the: http://localhost:8080/jaxwsprojectineclipse/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!!!
explained it...nice..server side and client code....for webservices...
ReplyDeleteOnline Microservices and docker training https://cloudnixiass.com/#courses
ReplyDeletehttps://cloudnixiass.com
Delete