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 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 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="" xmlns:wsdl=""

 xmlns:tns="" xmlns:xsd=""



  <xs:schema elementFormDefault="unqualified" targetNamespace=""

   version="1.0" xmlns:tns="" xmlns:xs="">

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

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

   <xs:complexType name="mul">


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

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



   <xs:complexType name="mulResponse">


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





 <wsdl:message name="mulResponse">

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



 <wsdl:message name="mul">

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



 <wsdl:portType name="MulSEI">

  <wsdl:operation name="mul">

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


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




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

  <soap:binding style="document"

   transport="" />

  <wsdl:operation name="mul">

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

   <wsdl:input name="mul">

    <soap:body use="literal" />


   <wsdl:output name="mulResponse">

    <soap:body use="literal" />




 <wsdl:service name="MultiplyWSService">

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


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




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 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
public class ServletClient extends HttpServlet {
 private static final long serialVersionUID = 1L;

  * @see HttpServlet#HttpServlet()
 public ServletClient() {
  // 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"))));  


  * @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 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 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

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 {}MultiplyWSService from WSDL: http://localhost:9083/WebServices/services/MultiplyWSPort?wsdl
Aug 11, 2012 7:47:19 PM
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
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.



Currently have 1 comments:

Leave a Reply