Saturday, June 6, 2015

Create Axis2 Web service (Bottom-Up Approach) using Eclips in Windows

As Apache organizations definition Axis2 is a Web Services / SOAP / WSDL engine, the successor to the widely used Apache Axis SOAP stack. In this tutorial I will describe how to create Axis2 web service using eclips in windows.

Prerequisites 
  1. You should have install java in your PC and set path variable correctly (JAVA_HOME, JRE_HOME)
  2. You should have Apache Tomcat and Axis2 download and extracted to you PC 
  3. You should have install Eclips Java EE IDE in your PC

Lets start the web service development 
First lets add Tomcat run time environment to Eclips. Go to Window->Preferences then select Server on your left hand side under that you can see run time environments and click add button in right hand side. You can see similar interface as Figure 1.
Figure 1:  Server Run Time environment wizard
Select Apache Tomcat v7.0 and click next. Then you can see similar interface ti Figure 2. In that interface click on Browse button and select the Apache Tomcat 7 Extracted folder. Then Click Finish

Figure 2:  Select Apache Tomcat extracted path
Then you can see the added environment as Figure 3.
Figure 3 : Added tomcat server
 Then you need to add Axis 2 path to the Eclips preferences. Again go to Window->Preferences then click on Web Services and then select Axis 2 Preferences. Then click on the Brows button and select the Extracted axis 2 folder path. (Figure 4)
Figure 4 : Axis 2 path set on Axis2 preferences 


Step 1 Create Dynamic web project
Click on file->New->Other and you can see interface like Figure 5 and type Dynamic in wizads input field and select the Dynamic Web Project. Then click next 

Figure 5:  Dynamic Web project selection

Then add project name as you preferred I'll put project name as SampleWebService (Figure 6) Then Change the Dynamic web module version to 2.5 and then Click on Modify button under configuration.
Figure 6: Provide project name in wizard
Then select Axis2 Web Services and click Ok button. (Figure 7)
Figure 7 : Select Axis2 web services
Then click next and click on Finished button. Then you can see interface similar to Figure 8

Figure 8: After initialize the Dynamic project in Eclips 

Step 2 Create Web service class
Then we have to write the service class that have the method that we need to implement as service. In my example I'll write method to multiply two number which pass as parameters.
Figure 9: Service Class content

Right click on the project you created and select new->Other (Figure 10)

Figure 10 : Right click on project and Select New Other
Then type service on wizards input box as figure 11 and select the web service 

Figure 11: Select Web Service from wizard
Then You will see interface similar to Figure 12 and give a service name as you preferred and then click on Web Service runtime: Apache Axis
Figure 12: Select service class you select and then click web service runtime

Then change the web server run time to Apache Axis2.


Figure 13: Select Apache Axis2 as Web service run time

Then Click next and in next interface make sure to select Generate a default service.xml file option. (Figure 14)


Figure 14: Make sure to select Generate a default service.xml file
Then click Next button and then in next interface you have to click Start Server button as show in Figure 15. Then Click Finish button.


Figure 15: Click Start Server
After you finish you can see log similar to Figure 16. 


Figure 16: Service successfully started on console
As show in Figure 16 you can find the port your service runs in my example it's run on 8080 port(Circled in Figure 16 ). Then you can access web service you have just run by go to following link.
http://localhost:8080/SampleWebService/services/listServices
Figure 17: All your services run on Apache
Click on the Service class name that show in the page and you can access the wsdl file.


Figure 18: WSDL loaded in the web browser 

Step 3 Create Axis Archive file
Now we have successfully run the web service and but we haven't create Asix Archive file from web service that we created. To create Axis archive go to the following path YourProjectNamePath\WebContent\WEB-INF\services\ServiceYouCreated using command line. Then enter following command.

jar cvf FirstService.aar com META-INF


Figure 19: Command successfully run


Then you can see FirstService.aar file has created on your folder location.(Figure 20)


Figure 20: Generated Axis Archive file
Then lets deploy the Axis2.war file in Tom cat. You should copy the Axis2.war file in to webapps folder on Apache tomcat. Then go to the bin folder on Apache tomcat installation directory from command prompt. Then run startup.bat. Please not that in this point you have to set JAVA_HOME and JRE_HOME correctly in Environment Variables.  

Then you can access the http://localhost:8080/axis2/ and you can see interface similar to Figure 21.
Figure 21: Axis2 on web browser
Then Click on Administrator and you will ask the user name and password. Enter user name and password.(Default username admin and password axis2) Then you can find interface similar to Figgure 22. Then Click on Upload Service in Top left in your page.


Figure 22: Click Upload service on left top corner in the page


Figure 23: Click on Choose file and select the Axis Archive File (FirstService.arr)
Then go to Available service section you can see the your service as Figure 24. Then you can click on the web service you created and access the WSDL link.


Figure 24: Available services

Step 4 Create Web service Client

Then we have to create client stub class which we can use to access the web service methods. You can right click on project then New->Other then select Web Service Client. (Figure 25)



Figure 25: Select Web Service Client
 In the interface in service definition give the WSDL path (for my project it's http://localhost:8080/SampleWebService/services/FirstService?wsdl ) then click on Web Service Runtime and select Axis2. Then click next. (Figure 26)


Figure 26: Provide wsdl path and select axis2 web service run time
Next interface you can provide package name for the class which we are going to generate. Then click Finish Then you can see the newly created class in the package you specify.


Figure 27: Stub classes generated
Then create java project File->New->Other Java Project and copy the clientstub class in to that project. The write sample test class to run the web service client.

package it.com.clientapp;
import java.rmi.RemoteException;

import org.apache.axis2.AxisFault;

import it.com.clientstub.*;
import it.com.clientstub.FirstServiceStub.MultyTwoNumber;
import it.com.clientstub.FirstServiceStub.MultyTwoNumberResponse;

public class TestWebServie {
public static void main(String[] args) {
try {
FirstServiceStub stub=new FirstServiceStub();

MultyTwoNumber num=new MultyTwoNumber();
num.setNum1(10);
num.setNum2(16.2);
MultyTwoNumberResponse response=stub.multyTwoNumber(num);
double returnVal=response.get_return();
System.out.println("Client values are :"+ 10 + " x "+16.2);
System.out.println("Server returns "+returnVal);
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

Then right click on the class Run As -> Java Application. Then you can see this client application call the service method and returns the out put.

Figure 28: Client app Successfully  runs and returns the values from service

This is simple two numbers multiplication service implementation and your can implement your own service logic and different kind of parameters to the service. Hope this might help some one who in the early level on web service learning. 

Added on 2017-JAN

When Call from client if you received a error message as bellow then follow the bellow instructions to resolve it.

The ServiceClass object does not implement the required method in the following form: OMElement login(OMElement e)

This means in your services.xml file (inside the arr under META-INF) you need to do following changers due to Axis server version from 1.7.0

http://www.w3.org/2004/08/wsdl/XXX MEP URIs are no longer supported. Use http://www.w3.org/ns/wsdl/XXX instead


No comments:

Post a Comment