Thursday, May 9, 2013

Web Services using Apache CXF

1. Download and Install Eclipse, Tomcat and CXF


2. How to configure Tomcat in Eclipse

window->
preference->

server->

add->

runtime env->

Apache Tomcatv7->

Next->add apacheTomact root path->install jre

->add

->standard vm->

next->

directory->

add jdk root path->

 select default jdk->

ok->

select jdk->
                                                                               finish->ok




3. Configure CXF in Eclipse


1.

window->

preferences->

webservices->

cxf 2.x preferences->

add->

browser->

add cxf root path(C:\java\Apache-cxf-2.3.6)->

ok->

check2.3.0version->ok





2.

window->

preferences->

web services->

server and runtime->

web service runtime(Apache cxf 2.x)->ok




5. Create a Web Service in Eclipse


·         Create new Dynamic Web Project  [Project Name: DeepThoughtWS]

·         Dynamic web module version(2.5)

·         Configuration(CXF Web Services Project v2.5)

·         Finish



Create java imporation

·         select project DeepThoughtWS

·         java Resources src->new->class->package(org.yash.ws)->Name(DeepThought)->finish



Open DeepThought.java and add below code.

package org.yash.ws;

public class DeepThought{

                public String whatIsTheAnswer(String interviewer){

                                return "The answer, "+ interviewer + "is 42";

                }



}



Now open Toolbar NEW option->webservice->browser

->type class you want to select(DeepThought class)->select both sliders to full

->select Monitor the web service->next->select use a service endpoint interface

->select create an SEI->write(DeepThoughtSEI)

->then select method whatIsTheAnswer

->next(this page ask to configure annotation) skip this

->next

->next

->click start server

->next->next->next ->next->next

->finish



WebService page will appear

-> click on link "whatIsTheAnswer"

-> click on "ADD" text box will appear->type Yash

->Go (Service will display "The answer, Yash is 42")(check SOURCE)





Open client project (DeepThoughtWSClient)

java Resources src->

org.yash.ws (all code which is require  generated here  to access WebService -  in this pkg)

->open file "DeepThoughtSEI_DeepThoughtPort_Client" update _whatIsTheAnswer_arg0 to "Yash"

->right click in class on run on as Java Application, result will be in console



Access the URL in browser




Create new project in SOAP UI tool using above wsdl URL.

Below Request and Response will come

Request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.yash.org/">

   <soapenv:Header/>

   <soapenv:Body>

      <ws:whatIsTheAnswer>

         <!--Optional:-->

         <arg0>Test</arg0>

      </ws:whatIsTheAnswer>

   </soapenv:Body>

</soapenv:Envelope>



Response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

   <soap:Body>

      <ns2:whatIsTheAnswerResponse xmlns:ns2="http://ws.yash.org/">

         <return>The answer, Test is 42</return>

      </ns2:whatIsTheAnswerResponse>

   </soap:Body>
</soap:Envelope>


Download WebService Document

No comments:

Post a Comment