Thursday 22 March 2012

Apache CXF - Developing Web Service with Eclipse



Required Software –

Apache CXF
Eclipse
Tomcat (6 and above)
JDK 1.6

Extract eclipse-jee-win32.zip as D:\demo\ws-cxf-support sw\eclipse
Extract apache-tomcat-x.x.x.zip as D:\demo\ws-cxf-support sw\tomcat
Extract apache-cxf-2.x.x.zip as D:\demo\ws-cxf-support sw\Apache CXF

TOMCAT_HOME = D:\demo\ws-cxf-support sw\tomcat
APACHE_CXF_HOME = D:\demo\ws-cxf-support sw\Apache CXF
JAVA_HOME = D:\demo\ws-cxf-support sw\JDK1.6
Create a workspace  as D:\demo\workspace

Configure Apache Tomcat as Follows : -
1. Select Window
2. Select Window->Preferences
3. Select Window->Preferences->Server
4. Select Server->Runtime Environment
5. Click On Add
6. Select the Apache Tomcat (For Respected Version ) and Click On Next
7. Select JDK1.6 as a part of Installed JRE
8. Select Tomcat Home directory See above TOMCAT_HOME


Configure Apache CXF as Follows : -
1. Select Window
2. Select Window->Preferences
3. Select WebService->CXF2.X Preferances
4. Select Add
 Select Apache CXF home See above APACHE_CXF_HOME
 Select Configure Apache CXF and Click on 2nd Tab (Java2WS)
 Select Configure Apache CXF and Click on 3rd Tab (WSDL2Java)
 Select Configure Apache CXF and Click on 4th Tab (JAX-WS)
 Select Configure Apache CXF and Click on 5th Tab (Endpoint Config)
 Select Use CXF Servlet

Getting Started with WebService Project (Publisher): -
1. Create a Web Service
     Right Click in Package Explorer
     Select New  Dynamic Web Project
     Enter Project Name as - WebServiceServer
     Target runtime – Apache Tomcat v
     Dynamic Web module Version – 2.5
     Configuration – CXF Web Services Project v2.5
     Click On Finish
     
2.   Enter Project Name – WebServiceServer


      Select Target RunTime as – Configured Tomcat Server




3.   Add package name com.ws


     Add a new class as a implementation for the service (ShowNameImpl.java)
     Click on finish
     Open a above class
       Add a method as a part of above class.
     
      public String getName(String name){
         return "Test "+name;
    }
4.   Select  Project , Right click on project name
5.  Select webservice
6.   Select Bottom up java bean web service
      Enter Service implementation class com.ws.ShowNameImpl
      Checked Monitor web service
7.   Click on Next     
8.  Checked Use a Service Endpoint Interface
     Select Create an SEI – enter Interface as IShowName
     Checked a method
      Click on Next
9.  Click on Next
10. Click on Next 
11. Click on Start Server
12.  Click on Next
13. Click On Next
14.  Click on Finish
15.  Open WebService Explorer , Click on method Name getName()
16.  Click on add – Enter Name – Shrikant
      Click on “Go” button

Request.xml generated on server log
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:getName>
      <q0:name>shrikant</q0:name>
    </q0:getName>
  </soapenv:Body>
</soapenv:Envelope>



Response.xml generated on server log
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
     <ns2:getNameResponse xmlns:ns2="http://ws.com/">
         <return>Test shrikant</return>
     </ns2:getNameResponse>
  </soap:Body>
</soap:Envelope>
Getting Started with WebService Project (Consumer): -
  1. Click on New->Dynamic Web Project
    ProjectName – WebServiceClient
    Target runtime – Apache Tomcat v
    Dynamic Web module Version – 2.5
    Configuration – CXF Web Services Project v2.5
    Click On Finish
 2.  Select wsdl folder from WebServiceServer/WEB-INF/wsdl
         Copy to WebServiceClient/WEB-INF/wsdl
     3.   Right Click on wsdl/shownameimpl.wsdl
         Select Web Services
         Select Generate Client and Click
     4.   Checked Monitor Web Service
            Click on Next
      5.   Click on Next
     6. Click On Next
        7.  Click On Next
     8.    Click on Start Server
                 Click On Finish
       9.   Click on Finish
10.   Check For the entire java file as a part of com.ws.*
11.    Select   IShowName_ShowNameImplPort_Client.java

                          ("Invoking getName...");

java.lang.String _getName_arg0 =

"Shrikant";




java.lang.String _getName__return = port.getName(_getName_arg0);
System.
out.println("getName.result=" + _getName__return);


And Run As Java Application.


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <q0:getName>
      <q0:name>shrikant</q0:name>
    </q0:getName>
  </soapenv:Body>
</soapenv:Envelope>




No comments:

Post a Comment