Prerequisites
- JDK should be installed in your computer
You can download the latest Axis2 binary distribution from the Apache Axis2 website (Axis2 Download Page). Latest stable release is Axis2 1.6.2. Download that and extract it into any place that you like. When will you extract, it will be looks as follows.
After extracting the Axis2, it is better to set the AXIS2_HOME environment variable in the system.
$AXIS2_HOME should be set to location where you extract the Axis2.
e.g. $AXIS2_HOME = /home/axis2-1.6.2
Creating Apache Archive (.aar) File
After setting up the environment let see how we can develop a simple web service using Axis2. I'm not going to use ant IDEs. But you can use any IDE that you are familiar with. You can use Axis2 plugins that are available for almost all the popular IDEs like Ecliple, IntelliJ and Netbeans too.
Open your favorite text editor/IDE and create a java class with following code.
public class HelloAxis{
public String sayHello(String name){
return "Hello " + name;
}
}
Save this with appropriate name.
e.g. here file name must be HelloAxis.java
Then you should get the compiled class file of the created file (HelloAxis.class).
For the clarity I'm copying that file to a new folder. Create a folder with the name META-INF in the same folder. Then create file with the name services.xml inside the META-INF folder and copy following code segment to that file.
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloAxis">
<Description> Hello Axis2 Web Service </Description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">HelloAxis</parameter>
<operation name="sayHello"/>
</service>
Note
In the above code ServiceClass value should be your class full qualified name and operation name should be the method name that you are exposing from the class.
After that your folder structure looks like as follows
Then open the terminal/CMD go to the particular path. And type following command in the terminal
jar -cvf HelloAxis.aar ./*
This will create a HelloAxis.aar in your folder that is what we should deploy.
Deploying Created .aar File
Apache Axis2 can be run as a standalone server or inside a application server like Apache Tomcat. Lets see how to deploy the created archive file in the standard Apache Axis2 server.
To start the Apache Axis2 server got to the bin folder in the Apache axis2 and execute followling command from the terminal/cmd. If you have set the $AXIS2_HOME environment variable you can start easily.
Linux
sh axis2server.sh
Windows
axis2server.bat
Note
In the default configuration server will be started in port 8080. You will see some errors if the port is already using by some other application. If so close them and retry or change the default server port to some other port using axis2.xml file in the conf folder.
You will see a segment like
<transportReceiver name="http" class="org.apache.axis2.transport.http.SimpleHTTPServer">
<parameter name="port">8080</parameter>
Change the port value to some other port that you like.
If the server get started successfully you can see something like below image on your screen. Don't close the terminal.
Now copy the created HelloAxis.aar file into repository/services path inside the Axis2 folder
e.g.
/home/thusitha/axis2-1.6.2/repository/services
When you copied the file, and you followed the steps as told, you will see a similar log entry as follows in the Terminal.
This means that our webservice have been successfully deployed to the Axis2 server.
Testing the Service
Open the web browser and type following URL
http://localhost:8080/axis2/services/HelloAxis/sayHello?name=Thusitha
You will output like
<ns:sayHelloResponse xmlns:ns="http://ws.apache.org/axis2">
<ns:return>Hello Thusitha</ns:return>
</ns:sayHelloResponse>
Note
check the WSDL e.g. (http://localhost:8080/axis2/services/HelloAxis?wsdl) section relate to sayHello method. It should be looks like
<xs:element name="sayHello">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
name should be your variable name. If it is like args0 or param0, you must recompile the class using -g flag.
e.g.
javac -g HelloAxis.java
Then follows the same steps.
No comments:
Post a Comment