রবিবার, ৫ সেপ্টেম্বর, ২০১০

Axis2 WebService with eclipse europa [In 9 simplest steps]



I am sharing my experience. Special thanks to Md. Monowar Alam vai. He helped me understand the basics of axis2 and write this tutorial.

Requirements:
1. Eclipse [I am using eclipse europa version 3.3.0]
2. Tomcat [I am using Tomcat 5.5: Installed at C:\Tomcat5.5]
3. Axis2 [I am using axis2 1.4.1; jars are at I:\axis2\axis2-1.4.1\lib; axis2.war is at I:\axis2\axis2-1.4.1-war]

Assumed Tomcat is installed and configured with Eclipse.

Step 1: Go to Eclipse: Window -> Preferences -> Web Services -> Axis2 Preferences
Select Axis2 Runtime Location [In my case it is I:\axis2\axis2-1.4.1]



Step 2:
Create a new Java Project [Look not a web service]. Name it 'myws'.
Create a new package in the 'src'. Name it 'demo'.
Create a new Java class. Name 'Adder'.
Create a method 'sum'

Adder.java
---------------

package demo;
public class Adder {
public int sum (int a, int b) {
return a+b;
}
}

Step 3:
Create a folder inside 'src'. Name 'META-INF'
Create a file inside 'src/META-INF', named 'services.xml'

services.xml
-------------------

<service name="myws" >
<description>
Please Type your service description here
</description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">demo.Adder</parameter>
</service>

Since 'myws' is only one service so here [<service name="myws" >] name attribute is optional.

Our method has two arguments and one RETURN VALUE. Since we have return value that is it is not void so

<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />

is also not necessary. But if they are there NO Problem at all.


Step 4:

Right click on the project -> Properties -> Java build path -> Libraries
Add the axis2 jars [In my case they are at at I:\axis2\axis2-1.4.1\lib]



Step 5:

Copy axis2.war file [In my case which was in I:\axis2\axis2-1.4.1-war ] and paste it inside Tomcat/webapps [In my case C:\Tomcat5.5\webapps]

Run Tomcat.

War file will be extracted. I have got a folder named axis2.

Inside axis2/WEB-INF/services create a folder named 'myws'. We will point our project's output here in the next step.

Step 6:
We will now change default output location to Tomcat/webapps/axis2/WEB-INF/services/myws

Right click on the project -> Properties -> Java build path -> Source
Click Browse button beside Default output location
Click on the project name
Click 'create new folder' button
Name 'axis2' and Click 'Advanced' button.
Check the box 'Link to the folder in the file system'



Browse and select the directory [C:/Tomcat5.5/webapps/axis2/WEB-INF/services/myws].

Click ok all the times and back to the editor.

A prompt will come and ask if the output location's change will be accepted or not. Click ok.

Start or restart the tomcat.

Now output of the project should come at the directory [C:/Tomcat5.5/webapps/axis2/WEB-INF/services/myws]






If the output does not come please clean the project and restart the tomcat again. Output must have to come there.

Step 7:
Open the browser.
Go to the url [http://localhost:8080/axis2/services/listServices]
You will see the service 'myws' there.


Click on the link. You will be directed to the wsdl page. The url of this page is needed to create the Client. So copy the url for the next step.

Now the service is done.
Let's create a client for this server.

Step 8:

From Eclipse File -> New -> Other -> Web Services -> Web Service Client
Paste the url that you have copied in step 7 in the 'service definition'


Select "Web Service runtime: Apache Axis2" and select 'Apache Axis2'.

Select "Web Service Project" and rename the default with "mywsClient".



Select Next and follow whatever to do and finally Finish.

A client will be created in the eclipse.

Step 9:

In the client you will see two classes.
1. MywsCallbackHandler.java
2. MywsStub.java

Create a class Test.java to test the web service.

Test.java
----------------------

public class Test {

public static void main(String[] args) {
try {
MywsStub stub = new MywsStub();
Sum sum = new Sum();
sum.setA(10);
sum.setB(20);
SumResponse sumValue = stub.sum(sum);
int retValue = sumValue.get_return();
System.out.println(retValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}

And that's it.

Run the program you will get output 30 if everything is ok.







0 মন্তব্য(সমূহ):

একটি মন্তব্য পোস্ট করুন