Apache Axis2 is a SOAP processing engine and its main function is to deliver incoming SOAP messages into target applications, and in this context application is a Web service. This tutorial provides step by step guidance on how to create your first Hello World application.
Prerequisite:
- Java Development Kit (Follow this link to install on Ubuntu)
- Apache Axis2 (Follow this link to setup on Ubuntu)
Step 1:
Create a new class HelloService as shown below.
public class HelloService {
/**
* This method will be the add operation of the web service.
*/
public int add(int x, int y) {
int ans = x + y;
return ans;
}
}
Step 2:
Create a new services.xml as shown below.
<service>
<parameter locked="false" name="ServiceClass">HelloService</parameter>
<operation name="add">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
In this xml file, HelloService is your class name and the operation name is the name of your method.
Step 3:
Compile the HelloService.java.
Create a folder named service and store the HelloService.class directly inside that folder and the services.xml under service/META-INF directory.
service
|-- HelloService.class
|-- META-INF
|-- services.xml
Step 5:
Open the terminal in the service directory and enter the following command. It will create a HelloService.aar file in the service directory.
jar -cvf HelloService.aar *
Step 6:
Copy the HelloService.aar to $AXIS2_HOME/repository/services directory.
Step 7:
Run Axis2 Server by running the following command.
Linux users:
sh $AXIS2_HOME/bin/axis2server.sh
Windows users:
%AXIS2_HOME%\bin\axis2server.bat
After the Axis2 server is up and running, visit to http://localhost:8080/axis2/services/ in your browser. You will see the available services listed there including HelloService. Now your web service is ready to service the client requests.Step 9:
From this step onwards, we will create a client for our HelloService. Create a new folder Client, open the terminal in that folder and run this command.
Linux users:
sh $AXIS2_HOME/bin/wsdl2java.sh -uri http://localhost:8080/axis2/services/HelloService?wsdl -o .
Windows users:
%AXIS2_HOME%\bin\wsdl2java.bat -uri http://localhost:8080/axis2/services/HelloService?wsdl -o .
This command will create the client side stub and holder classes for the HelloService.Step 10:
Open the src folder which is created in Step 9 and create a new class Client.java inside that folder.
import org.apache.ws.axis2.*;
public class Client {
public static void main(String[] args) throws Exception {
// Create the stub object
HelloServiceStub stub = new HelloServiceStub();
// Create the request
HelloServiceStub.Add request = new HelloServiceStub.Add();
// Set the parameters
request.setArgs0(10);
request.setArgs1(20);
// Invoke the service
HelloServiceStub.AddResponse response = stub.add(request);
int ans = response.get_return(); // 10 + 20 = 30
System.out.println("Response : " + ans);
}
}
This client sends number 10 and 20 to the HelloService add method and prints the response received from that service.
Step 11:
Compile and run the Client using the following commands from src folder.
Linux users:javac -cp .:$AXIS2_HOME/lib/* Client.java
java -cp .:$AXIS2_HOME/lib/* Client
Windows users:
javac -cp .;%AXIS2_HOME%/lib/* Client.java
java -cp .;%AXIS2_HOME%/lib/* Client
You must see an output saying Response: 30.
Find the code at Git Hub.
1 comments:
Write commentsThis article was really useful. keep going....
ReplyEmoticonEmoticon