Saturday, August 11, 2012

Web Services Development using Eclipse, Apache CXF 2 Part-2

Posted by Shiva Gouraram | Saturday, August 11, 2012 | Category: |

Hi TGMCians this is a second part of tutorial series of WebServices development using Eclipse and CXF2.
if you have not read the first part please read it here.

Web Services Development using Eclipse, Apache CXF 2

O.K...in this tutorial we are going to develop a servlet which acts as a client for the developed webservice.
this tutorial is not available in the internet...so i am trying to do that for TGMCians.
Before developing our you have to check whether your service deployed correctly in tomcat or not..for checking that point your browser to locate the URL 
here you will find all the available web services which are developed using CXF in your application.

You can find a screen like this where you will find your WebServices WSDL URL.Click on it.


After clicking on the link a WSDL file opens in the browser which is typically a XML file which contains the binding details of the web service its port numbers and other details.

Your WSDL File looks like the below one.

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions name="MultiplyWSService"

 targetNamespace="http://shiva.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

 xmlns:tns="http://shiva.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

 xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">

 <wsdl:types>

  <xs:schema elementFormDefault="unqualified" targetNamespace="http://shiva.com/"

   version="1.0" xmlns:tns="http://shiva.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="mul" type="tns:mul" />

   <xs:element name="mulResponse" type="tns:mulResponse" />

   <xs:complexType name="mul">

    <xs:sequence>

     <xs:element name="arg0" type="xs:int" />

     <xs:element name="arg1" type="xs:int" />

    </xs:sequence>

   </xs:complexType>

   <xs:complexType name="mulResponse">

    <xs:sequence>

     <xs:element name="return" type="xs:int" />

    </xs:sequence>

   </xs:complexType>

  </xs:schema>

 </wsdl:types>

 <wsdl:message name="mulResponse">

  <wsdl:part name="parameters" element="tns:mulResponse">

  </wsdl:part>

 </wsdl:message>

 <wsdl:message name="mul">

  <wsdl:part name="parameters" element="tns:mul">

  </wsdl:part>

 </wsdl:message>

 <wsdl:portType name="MulSEI">

  <wsdl:operation name="mul">

   <wsdl:input name="mul" message="tns:mul">

   </wsdl:input>

   <wsdl:output name="mulResponse" message="tns:mulResponse">

   </wsdl:output>

  </wsdl:operation>

 </wsdl:portType>

 <wsdl:binding name="MultiplyWSServiceSoapBinding" type="tns:MulSEI">

  <soap:binding style="document"

   transport="http://schemas.xmlsoap.org/soap/http" />

  <wsdl:operation name="mul">

   <soap:operation soapAction="urn:Mul" style="document" />

   <wsdl:input name="mul">

    <soap:body use="literal" />

   </wsdl:input>

   <wsdl:output name="mulResponse">

    <soap:body use="literal" />

   </wsdl:output>

  </wsdl:operation>

 </wsdl:binding>

 <wsdl:service name="MultiplyWSService">

  <wsdl:port name="MultiplyWSPort" binding="tns:MultiplyWSServiceSoapBinding">

   <soap:address

    location="http://localhost:9083/WebServices/services/MultiplyWSPort" />

  </wsdl:port>

 </wsdl:service>

</wsdl:definitions>







Now we need to create a Servlet which invokes a web service method from a web browser.
Create a servlet Names "ServletClient" in "com.servlet" package.

package com.shiva.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shiva.MulSEI;
import com.shiva.MultiplyWSService;

/**
 * Servlet implementation class ServletClient
 */
@WebServlet("/ServletClient")
public class ServletClient extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#HttpServlet()
  */
 public ServletClient() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub


  MultiplyWSService service = new MultiplyWSService();//Creating 
  MulSEI port = service.getMultiplyWSPort(); 
  PrintWriter out=response.getWriter();
  
  out.println(port.mul(Integer.parseInt(request.getParameter("one")), Integer.parseInt(request.getParameter("two"))));  
  out.flush();



 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}




Here we have to notice three main statements which are used to Invoke a webService method.

 1) MultiplyWSService service = new MultiplyWSService();

Which is used to instantiate the servicing class of the Web Service ...here it is "MultiplyWSService".



2)MulSEI port = service.getMultiplyWSPort();

to get the port number of the WebService we have to call the "getMultiplyWSPort()" on the service object of "MultiplyWSService".



3)port.mul(Integer.parseInt(request.getParameter("one")), Integer.parseInt(request.getParameter("two")))

Here we are invoking the actual method of a WebServe which serves us...here we are calling mul method on the Service End Point Interface object "port".

That's it...we have now created our servlet which acts as web service client.


Now you can open this servlet to test whether we have developed correctly or not
http://localhost:9083/WebServices/ServletClient?one=3&two=5






Here you can see the actual communication between our client and server is in the form of SOAP messages which is an XML based protocol through which our application communicates to the server or a web service.

Aug 11, 2012 7:47:19 PM org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
INFO: Creating Service {http://shiva.com/}MultiplyWSService from WSDL: http://localhost:9083/WebServices/services/MultiplyWSPort?wsdl
Aug 11, 2012 7:47:19 PM org.apache.cxf.services.MultiplyWSService.MultiplyWSPort.MulSEI
INFO: Inbound Message
----------------------------
ID: 6
Address: http://localhost:9083/WebServices/services/MultiplyWSPort
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: {Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[186], content-type=[text/xml; charset=UTF-8], host=[localhost:9083], pragma=[no-cache], SOAPAction=["urn:Mul"], user-agent=[Apache CXF 2.5.1]}
Payload: 35
--------------------------------------
Aug 11, 2012 7:47:19 PM org.apache.cxf.services.MultiplyWSService.MultiplyWSPort.MulSEI
INFO: Outbound Message
---------------------------
ID: 6
Encoding: UTF-8
Content-Type: text/xml
Headers: {}
Payload: 15
--------------------------------------
That's it we are now developed a WebService and a servlet based client for invoking the service from a web interface you can develop any type of applications by making this tutorial as base. Here I am providing you the sample web application which we have developed now..It is a war file just directly deploy it in tomcat server.

Download WebServices.war

Hope it will be helpful to everyone.

.




Share/Bookmark

Currently have 1 comments:

Leave a Reply

Subscribe