Tuesday, August 16, 2011

JAX-WS(Java Api For Xml Based Web Service) Integration With Web Application

In previous post JAX-WS Tutorial we discussed about how to create web service using JAX-WS API and create a Simple SEI(Service Endpoint) and publish that service end point using simple java program on existing running web application, Now i am going to discussed with you,"How to integrate JAX-WS enabled service with your simple web application".

Often times, JAX-WS always be part of your Java web application. Here i show you how to integrate JAX-WS into Java web application easily,only some basic step which are as follows.

 1:-Directory structure of your web application
 

2:-create a simple Endpoint
package com.amit.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
 
@WebService
public class HelloWorld{
 
 @WebMethod(operationName="getHelloWorld")
 public String getHelloWorld(String name) {
  return "Hello World JAX-WS " + name;
 }
 
}
 

3:-create we service deployment descriptor(sun-jaxws.xml) in your WEB-INF folder

<?xml version="1.0" encoding="UTF-8"?>
<endpoints
  xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
  version="2.0">
  <endpoint
      name="HelloWorldWs"
      implementation="com.amit.ws.HelloWorld"
      url-pattern="/hello"/>
</endpoints>
 
4:-Make following entries on your web.xml file
  
1:-Defines “com.sun.xml.ws.transport.http.servlet.WSServletContextListener” as listener class.  
2:-Defines “com.sun.xml.ws.transport.http.servlet.WSServlet” as your web service (hello) servlet. 
      here is content of web.xml file,

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
 <display-name>Archetype Created Web Application</display-name>
 
 <listener>
  <listener-class>
   com.sun.xml.ws.transport.http.servlet.
                        WSServletContextListener
                </listener-class>
 </listener>
 <servlet>
  <servlet-name>hello</servlet-name>
  <servlet-class>
   com.sun.xml.ws.transport.http.servlet.WSServlet
                </servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>hello</servlet-name>
  <url-pattern>/hello</url-pattern>
 </servlet-mapping>
 
</web-app>
 
5:-Done
   The integration between JAX-WS and web application is done. Deploy it and access 
   via URL : http://localhost:8080/WebServicesExample/hello 
  
   here is the output look like this on your web browser,
Web Services


Endpoint Information
Service Name: {http://ws.amit.com/}HelloWorldImplService
Port Name: {http://ws.amit.com/}HelloWorldImplPort
Address: http://localhost:8090/HelloWorld-20110808/hello
WSDL: http://localhost:8090/HelloWorld-20110808/hello?wsdl
Implementation class: com.amit.ws.HelloWorldImpl


And when you click on http://localhost:8090/HelloWorld-20110808/hello?wsdl (wsdl uri) it will show you wsdl generated xml,here it is the generated wsdl code.



This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-b01-.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.1-b01-.
-->
<types/>
<message name="getHelloWorldAsString"/>
<message name="getHelloWorldAsStringResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="HelloWorld">
<operation name="getHelloWorldAsString">
<input wsam:Action="http://ws.amit.com/HelloWorld/getHelloWorldAsStringRequest" message="tns:getHelloWorldAsString"/>
<output wsam:Action="http://ws.amit.com/HelloWorld/getHelloWorldAsStringResponse" message="tns:getHelloWorldAsStringResponse"/>
</operation>
</portType>
<binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="getHelloWorldAsString">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://ws.amit.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://ws.amit.com/"/>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
</port>
</service>
</definitions>



                                @Amit Rawat