You are on page 1of 11

WEDNESDAY, JANUARY 25, 2012

To develop sample webservice application using Xfire


To know more about xfire http://xfire.codehaus.org/ Below are the steps to create a sample web service "AdditionWebService" (by using xfire) which takes input 2 numbers and returns sum of those numbers as response. To create new webservice project You need to have J2EE eclipse developer for below steps to follow In eclipse go to - File->New->Project search for web

select "Dynamic Web Project" , give Project Name, Next & Finish Folder structure once the project is created.

In .classpath change <classpathentry kind="output" path="build/classes"/> to <classpathentry kind="output" path="WebContent/WEB-INF/classes"/> Inorder to add JBOSS jar files to classpath Create User library "JBOSS4.3"

Go to eclipse->Window->Preferences->Java->Build Path->User Libraries Click on new -> (give any name) JBOSS4.3, OK Select JBOSS4.3 and select "Add JARs" go to <JBOSS-HOME>client For example - C:\jboss-eap-4.3\jboss-as\client select all jars and click open. Click ok Now add that user library to build path of the project Right click on the project, go to properties, in Java Build Path, In Libraries tab, click on "Add Library" select User library, click next and select "JBOSS4.3" Click OK, Finish.

Create request & response bean In /AdditionWebService/src/com/test/bean/AdditionRequest.java package com.test.bean; public class AdditionRequest { private int anumber; private int bnumber; public int getAnumber() { return anumber; } public int getBnumber() { return bnumber; } public void setAnumber(int anumber) { this.anumber = anumber; } public void setBnumber(int bnumber) {

this.bnumber = bnumber; } } In /AdditionWebService/src/com/test/bean/AdditionResponse.java package com.test.bean; public class AdditionResponse { private int cnumber; public int getCnumber() { return cnumber; } public void setCnumber(int cnumber) { this.cnumber = cnumber; } } Create interface In /AdditionWebService/src/com/test/service/AdditionWebService.java package com.test.service; import com.test.bean.AdditionRequest; import com.test.bean.AdditionResponse; public interface AdditionWebService { public AdditionResponse addNumbers(AdditionRequest addRequest); } Implementation In /AdditionWebService/src/com/test/service/AdditionWebServiceImpl.java package com.test.service; import javax.jws.WebService; import com.test.bean.AdditionRequest; import com.test.bean.AdditionResponse; @WebService public class AdditionWebServiceImpl implements AdditionWebService { public AdditionResponse addNumbers(AdditionRequest addRequest) { AdditionResponse addResponse = new AdditionResponse(); addResponse.setCnumber(addRequest.getAnumber()+addRequest.getBnumber()); return addResponse; } }

In /AdditionWebService/WebContent/WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/webapp_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>AdditionWebService</display-name> <!-- XFire servlet-related entries START --> <servlet> <servlet-name>XFire</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>XFire</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <!-- XFire servlet-related entries END --> </web-app> In /AdditionWebService/WebContent/META-INF/xfire/services.xml <beans xmlns="http://xfire.codehaus.org/config/1.0"> <service> <name>AddService</name> <namespace>addition</namespace> <serviceClass>com.test.service.AdditionWebService</serviceClass> <implementationClass>com.test.service.AdditionWebServiceImpl</implementationClass> </service> </beans>

Jars that needs to be downloaded for Xfire http://xfire.codehaus.org/Dependency+Guide You can download all jars mentioned in above link http://xfire.codehaus.org/Download download distribution zip file and In xfire-1.2.6\lib you will find all required jars Copy required jars mentioned in http://xfire.codehaus.org/Dependency+Guide to /AdditionWebService/WebContent/WEB-INF/lib To create war & deploy to JBOSS server Right click on project -> Export

Search for war

In Destination folder go to your deploy folder & click finish.

Once war is deployed goto http://localhost:8080/AdditionWebService/services/AddService?wsdl to generate wsdl file.

View source and copy to AddService.wsdl file To test service using soapUI client Create new soap UI project

Sample request and response xml

To test with Java client Generate client using wsdl

Select output folder -

Below files will be created.

In /AddServiceClient/src/com/test/client/AddServiceClient.java

package com.test.client; import addition.AddServiceLocator; import addition.AddServicePortType; import com.test.bean.AdditionRequest; import com.test.bean.AdditionResponse; public class AddServiceClient { private static AddServiceLocator service; private static AddServicePortType port; public static void main(String [] args) { try { // webservice end address service = new AddServiceLocator(); String webservice_address = "http://localhost:8080/AdditionWebService/services/AddService"; service.setAddServiceHttpPortEndpointAddress(webservice_address); port = service.getAddServiceHttpPort(); // create request Integer anumber = new Integer(3); Integer bnumber = new Integer(4); AdditionRequest addRequest = new AdditionRequest(); addRequest.setAnumber(anumber); addRequest.setBnumber(bnumber); // calling webservice method AdditionResponse addResponse = port.addNumbers(addRequest); System.out.println("Addtion service response : "+addResponse.getCnumber()); } catch (Exception e) { e.printStackTrace(); } } } Output Addtion service response : 7

You might also like