In my android application, I have used a web service as a server application. I have created a web service with Apache axis2, which provides details of nearest hospitals and police stations with respect to current location provided by the client application.
In this article, I would illustrate how to write a simple web service using Apache Axis2. First you have to download Apache axis2 binary distribution and install it. (Check whether you have installed java by giving 'java version' command before starting everything. If not download jdk and install java using installation guide provided with downloaded version.)
Let's write a simple web service to calculate the sum of two numbers. 'CalcSumService' is our web service and its simply a java class which has a method to calculate the sum of given two values.
public class CalcSumService { public int calcSum(int value1,int value2){ return value1+value2; } }
Then save the class as 'CalcSumService.java' and compile (using 'javac CalcSumService.jav '). Then 'CalcSumService.class' would be generated.
To host an web service in Apache axis2, we have to create a services.xml file to inform the Axis2 about the newly deploying service. The services.xml file should be contained the following code.
Now, all the elements need to host the web service are ready. First create a folder name META-INF and copy the 'services.xml' file into that folder. Then create a zip folder containing that META-INF directory and compiles CalcSumService.class. Rename that zip file as 'CalcSumService.aar'.
Otherwise, locate 'META-INF' and 'CalcSumService.class' in a directory called 'temp'. Then go inside that directory and execute 'jar -cvf CalcSumService.aar*'. Then the .aar file would be generated.
After successfully creating CalcSumService.aar file, go to axis2/repository/services directory and copy the 'CalcSumService.aar' file in to services directory.
Now our service is hosted. If we want to check whether the serviceis successfully hosted, we have to start the SimpleHTTPServer. For that go to axis2/bin and start the axis2server.
In Linux, give 'sh axis2server.sh' and in windows give 'axis2server.bat drive:/path to bin/' to run the server. Then in your web browser, enter the url ' http://localhost:8080/axis2/services/'. Then the deployed service would be visible for you. You can see the WSDL file of the hosted service by clicking the service name.
If the service is successfully deployed, you might see the service as shown below with service name and functions available.
To host an web service in Apache axis2, we have to create a services.xml file to inform the Axis2 about the newly deploying service. The services.xml file should be contained the following code.
<service name="CalcSumService" > <description>This is the HelloWorld service</description> <parameter name="ServiceClass">HelloWorld</parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/> </operation> </service>
Now, all the elements need to host the web service are ready. First create a folder name META-INF and copy the 'services.xml' file into that folder. Then create a zip folder containing that META-INF directory and compiles CalcSumService.class. Rename that zip file as 'CalcSumService.aar'.
Otherwise, locate 'META-INF' and 'CalcSumService.class' in a directory called 'temp'. Then go inside that directory and execute 'jar -cvf CalcSumService.aar*'. Then the .aar file would be generated.
After successfully creating CalcSumService.aar file, go to axis2/repository/services directory and copy the 'CalcSumService.aar' file in to services directory.
Now our service is hosted. If we want to check whether the serviceis successfully hosted, we have to start the SimpleHTTPServer. For that go to axis2/bin and start the axis2server.
In Linux, give 'sh axis2server.sh' and in windows give 'axis2server.bat drive:/path to bin/' to run the server. Then in your web browser, enter the url ' http://localhost:8080/axis2/services/'. Then the deployed service would be visible for you. You can see the WSDL file of the hosted service by clicking the service name.
If the service is successfully deployed, you might see the service as shown below with service name and functions available.
Using Eclipse IDE
We can use Eclipse IDE to host a web service. That is more easier than the above described procedure. Let's see how to write a simple web service and how to host it using axis2. First download Apache Axis2 Binary Distribution and Eclipse IDE for java EE developers . Then install Eclipse.
After installing Eclipse, open it. Now we have to set Axis2 Runtime. For that goto Windows> Preferenece and select 'web services' from drop down menu in the left side.
Then select Axis2 Preference from the drop down list. You will find the below like dialog box.
In that dialog box, under Axis2 Runtime, browse the location where your downloaded Axis2 folder is located. Then click ok. Now we have successfully set the Axis2 Runtime. Now we have to define a server to deploy the web service. Here I use Tomcat server to host the web service.
In Eclipse select File>New>Other... Then select the Servers from the drop down menu.
Then select Server. Then the below window will pop out to select a server.
Then the preferred server has to be selected. In my case I select Tomcat version 7, and click next. Then you'll find the below window to install Tomcat server which you have selected.
If you have downloaded Tomcat Server, just give the location where you have located it, using Browse. Otherwise Click on Download and install to install the requested Tomcat Server.
Our next step is to start a 'Dynamic web project' and implement our web service class. For that goto File>New>Other>Web>Dynamic Web Project as shown in the image below.
Click 'Next' and you will find the below window.
There, give a name for the Dynamic Web project and in Configuration. Then click finish.
Now your project will look like this.
Now right click on Java Resources > src . Go to Nev>class. Then create a java class name CalcSumService.java as in the diagram shown below. Give a package name also.
Now in the created java class, write a method called calcsum to calculate the sum of two given integers.
In that window, you have to select Server runtime and web service runtime in Configuration. In server runtime, select the Tomcat v7.0 server, and in web service runtime, select Apache axis2.
Then click ok and after successfully setting configuration, click finish.
Then 'Next'
'Start Server'
Then go to the URL : http://localhost:8080/CalcSumService/services/listServices . If the service is successfully deployed, you would find following page shown below and the CalcSumService would be visible to you.
In that dialog box, under Axis2 Runtime, browse the location where your downloaded Axis2 folder is located. Then click ok. Now we have successfully set the Axis2 Runtime. Now we have to define a server to deploy the web service. Here I use Tomcat server to host the web service.
In Eclipse select File>New>Other... Then select the Servers from the drop down menu.
Then select Server. Then the below window will pop out to select a server.
Then the preferred server has to be selected. In my case I select Tomcat version 7, and click next. Then you'll find the below window to install Tomcat server which you have selected.
If you have downloaded Tomcat Server, just give the location where you have located it, using Browse. Otherwise Click on Download and install to install the requested Tomcat Server.
Our next step is to start a 'Dynamic web project' and implement our web service class. For that goto File>New>Other>Web>Dynamic Web Project as shown in the image below.
Click 'Next' and you will find the below window.
There, give a name for the Dynamic Web project and in Configuration. Then click finish.
Now your project will look like this.
Now right click on Java Resources > src . Go to Nev>class. Then create a java class name CalcSumService.java as in the diagram shown below. Give a package name also.
Now in the created java class, write a method called calcsum to calculate the sum of two given integers.
public class CalcSumService { public int calcSum(int value1,int value2){ return value1+value2; } }Now our java class is ready. What we have to do next is to create the web service using that java class. For that , right click on the CalcSumService.java and select web services>create web services. Then you will find the following window.
In that window, you have to select Server runtime and web service runtime in Configuration. In server runtime, select the Tomcat v7.0 server, and in web service runtime, select Apache axis2.
Then click ok and after successfully setting configuration, click finish.
Then 'Next'
'Start Server'
Then go to the URL : http://localhost:8080/CalcSumService/services/listServices . If the service is successfully deployed, you would find following page shown below and the CalcSumService would be visible to you.
No comments:
Post a Comment