JAX-WS WebService as Web Archive .war by Tomcat



This tutorial, we will discussed more about, JAX-WS Web Service as Web Archive WAR on Tomcat).

Software/Hardware requirement: Eclips, Java 1.5+, 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.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);
}


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.GreetingWebServiceImpl"
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
Live the web project name as it is: jaxwsprj
Destination: \apache-tomcat-6.0.39\webapps\jaxwsprj.war

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 or http://localhost:8080/jaxwsprj/GreetingWebService?wsdl

<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>
<xsd:schema>
<xsd:import namespace="http://webservicejaxws.vinod.com/" schemaLocation="http://localhost:8080/jaxwsprj/GreetingWebService?xsd=1"/>
</xsd:schema>
</types>
<message name="sayWelcome">
<part name="parameters" element="tns:sayWelcome"/>
</message>
<message name="sayWelcomeResponse">
<part name="parameters" element="tns:sayWelcomeResponse"/>
</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="document"/>
<operation name="sayWelcome">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="GreetingWebServiceImplService">
<port name="GreetingWebServiceImplPort" binding="tns:GreetingWebServiceImplPortBinding">
<soap:address location="http://localhost:8080/jaxwsprj/GreetingWebService"/>
</port>
</service>
</definitions>

Client code: click

Don't tail a lie with your near and dear!

7 comments:

  1. How can a simple class like GreetingWebService.java becomes a servlet in JAX-WS webservices?

    In jboss, even we do not map to any servlet type in web.xml.

    ReplyDelete
    Replies
    1. Thanks Ramu: In web.xml we have com.sun.xml.ws.transport.http.servlet.WSServlet which work as deligate or as getway.. https://jax-ws.java.net/nonav/jax-ws-20-fcs/arch/com/sun/xml/ws/transport/http/servlet/WSServlet.html

      Delete
  2. What is the difference between Document Style and RPC style other than just validation?

    ReplyDelete
    Replies
    1. Thanks Ramu: Document Vs RPC Style, along with few more keyword LITERAL, ENCODED and WRAPPED, we have and with help of it we can generate WSDL, SOAP Request and Response. Apart from validation it will used for doing the marshalling and unmarshalling. e.g. java to xml > xml to .net app. I'll post more on it ASAP.

      Delete