This tutorial, we will discussed more about, JAX-WS (Java API for XML Web Service) as Web Archive WAR in Eclipse deployed on Tomcat.
Software/Hardware requirement:
Eclips
Java 1.6+
Tomcat 6+
Create Dynamic web project in eclipse
Open the eclips from desktop or from existing dire.
Click on the File > New > Dynamic Web Project > Next.
Enter the Project name: jaxwsprj
Click Next > Click Next > Finish
Let be context root and content directory as it is default or change as per your convenient.
Create Web Service Interface
Right click on the src folder located under Java Resources > Next > New > class
Enter the name: GreetingWebService
Package: com.vinod.webservicejaxws
Click on the Finish
GreetingWebService.java
package com.vinod.webservicejaxws;
import javax.jws.WebMethod;
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.RPC)
public interface GreetingWebService{
/**
* sayWelcome with parameter as string
*/
@WebMethod String sayWelcome(String sName);
}
Create Web Service Implementation
Right click on the src folder located under Java Resources > Next > New > class
Enter the name: GreetingWebServiceImpl
Package: com.vinod.webservicejaxws
Click on the Finish
package com.vinod.webservicejaxws;
import javax.jws.WebService;
import com.vinod.webservicejaxws.GreetingWebService;
/**
*
* @author Vinod Kumar
* This is service implementation class
*/
@WebService(endpointInterface = "com.vinod.webservicejaxws.GreetingWebService")
public class GreetingWebServiceImpl implements GreetingWebService {
@Override
public String sayWelcome(String sName) {
return "Welcome " + sName + " !";
}
}
Add the sun-jaxws.xml
Right click on the WEB-INF folder located under WebContent > File
name of file: sun-jaxws.xml
This is for supporting the jax ws compilation.
< ?xml version="1.0" encoding="UTF-8"?>
< endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="GreetingWebService" implementation="com.vinod.webservicejaxws.impl"
url-pattern="/GreetingWebService" />
< /endpoints>
Modify the web.xml
Just modify the web.xml file as mention bellow:
< ?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>jaxwsprj</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>
Download the jax-ws dependent jars
Copy these jar in /tomcat/lib from http://jax-ws.java.net/
Export the WAR in Tomcat
Right click on the jaxwsprj project > Export > War file
Verify the service
Open the any browser and enter the URL
http://localhost:8080/jaxwsprj/GreetingWebService
Note: Where as tomcat port is running on 8080
< 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.vinod.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservicejaxws.vinod.com/"
name="GreetingWebServiceImplService">
<types />
<message name="sayWelcome">
<part name="arg0" type="xsd:string" />
</message>
<message name="sayWelcomeResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="GreetingWebService">
<operation name="sayWelcome">
<input
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeRequest"
message="tns:sayWelcome" />
<output
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeResponse"
message="tns:sayWelcomeResponse" />
</operation>
</portType>
<binding name="GreetingWebServiceImplPortBinding" type="tns:GreetingWebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="rpc" />
<operation name="sayWelcome">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</input>
<output>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</output>
</operation>
</binding>
<service name="GreetingWebServiceImplService">
<port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
<soap:address location="http://localhost:8080/jaxwsprj/GreetingWebService" />
</port>
</service>
< /definitions>
Happy learning and implementation.
Software/Hardware requirement:
Eclips
Java 1.6+
Tomcat 6+
Create Dynamic web project in eclipse
Open the eclips from desktop or from existing dire.
Click on the File > New > Dynamic Web Project > Next.
Enter the Project name: jaxwsprj
Click Next > Click Next > Finish
Let be context root and content directory as it is default or change as per your convenient.
Create Web Service Interface
Right click on the src folder located under Java Resources > Next > New > class
Enter the name: GreetingWebService
Package: com.vinod.webservicejaxws
Click on the Finish
GreetingWebService.java
package com.vinod.webservicejaxws;
import javax.jws.WebMethod;
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.RPC)
public interface GreetingWebService{
/**
* sayWelcome with parameter as string
*/
@WebMethod String sayWelcome(String sName);
}
Create Web Service Implementation
Right click on the src folder located under Java Resources > Next > New > class
Enter the name: GreetingWebServiceImpl
Package: com.vinod.webservicejaxws
Click on the Finish
package com.vinod.webservicejaxws;
import javax.jws.WebService;
import com.vinod.webservicejaxws.GreetingWebService;
/**
*
* @author Vinod Kumar
* This is service implementation class
*/
@WebService(endpointInterface = "com.vinod.webservicejaxws.GreetingWebService")
public class GreetingWebServiceImpl implements GreetingWebService {
@Override
public String sayWelcome(String sName) {
return "Welcome " + sName + " !";
}
}
Add the sun-jaxws.xml
Right click on the WEB-INF folder located under WebContent > File
name of file: sun-jaxws.xml
This is for supporting the jax ws compilation.
< ?xml version="1.0" encoding="UTF-8"?>
< endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint name="GreetingWebService" implementation="com.vinod.webservicejaxws.impl"
url-pattern="/GreetingWebService" />
< /endpoints>
Modify the web.xml
Just modify the web.xml file as mention bellow:
< ?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>jaxwsprj</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>
Download the jax-ws dependent jars
Copy these jar in /tomcat/lib from http://jax-ws.java.net/
Export the WAR in Tomcat
Right click on the jaxwsprj project > Export > War file
Start the tomcat from /tomcate/bin/startup.bat
Verify the service
Open the any browser and enter the URL
http://localhost:8080/jaxwsprj/GreetingWebService
Note: Where as tomcat port is running on 8080
Click on link, wsdl will display as below:
< 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.vinod.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservicejaxws.vinod.com/"
name="GreetingWebServiceImplService">
<types />
<message name="sayWelcome">
<part name="arg0" type="xsd:string" />
</message>
<message name="sayWelcomeResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="GreetingWebService">
<operation name="sayWelcome">
<input
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeRequest"
message="tns:sayWelcome" />
<output
wsam:Action="http://webservicejaxws.vinod.com/GreetingWebService/sayWelcomeResponse"
message="tns:sayWelcomeResponse" />
</operation>
</portType>
<binding name="GreetingWebServiceImplPortBinding" type="tns:GreetingWebService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="rpc" />
<operation name="sayWelcome">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</input>
<output>
<soap:body use="literal" namespace="http://webservicejaxws.vinod.com/" />
</output>
</operation>
</binding>
<service name="GreetingWebServiceImplService">
<port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
<soap:address location="http://localhost:8080/jaxwsprj/GreetingWebService" />
</port>
</service>
< /definitions>
Finishing Word:
Thanks! Put your input if any. Like and share it, if my work looks good.Happy learning and implementation.
No comments:
Post a Comment