Sunday, January 12, 2014

Writing Axis2 Services and deploying in Tomcat

There are three different ways which we can write Axis2 services (Web service  )  and can be deployed in Axis2 Server itself or in a web container like Tomcat.

Different ways of deploying services


  1. As simple POJO class in ..\webapps\axis2\WEB-INF\pojo\HelloWorld.class
  2. As JAR bundle  in ..\webapps\axis2\WEB-INF\pojo\myservice.jar
  3. As a service archive in  ..\webapps\axis2\WEB-INF\services\myservice.aar

We will focus on deploying service as AAR archive, which is best out of 3 deployment types mentioned above.


Getting Ready:

- Download and install Java (Minimum version is JDK1.5). Set the JAVA_HOME environment .
- Install latest version of  Tomcat in your environment ( http://tomcat.apache.org), and verify working properly. ( Sample code on this blog was test with Tomcat 7 as it was the latest stable version)

Download and install Axis2 module in tomcat


1. Download Axis2 WAR Distribution from below location. ( Version 1.6.2  was the latest at this time )

2. Deploy the Axis2 War into tomcat as a web application

- Unzip the axis2-1.6.2-war.zip
- You will get axis2.war file , then unzip this again. you will get axis2 folder containing axis2 deployable web app.
- Now copy the "axis2" folder into <TOMCAT_HOME>/webapps

3. Now start the Tomcat if it is down already, and verify the axis2 default services are running.

- Check available services in axis2 module  http://localhost:8080/axis2/services/listServices
- You will notice the getVersion application is running.
- Verify the webservice is running properly by invoking http://localhost:8080/axis2/services/Version/getVersion
You will get the axis2 version details in your screen  :) , you have verify your web service with REST style

If you need to verify this as Web-Service call using SOAP UI or Java client you can get WSDL with from http://localhost:8080/axis2/services/Version?wsdl

Writing our sample service

Lets write our own sample Web-Service using simple button up method ,where we have not even required to write any WSDL rather just know the java methods/operations needs to be exposed.

1. Writing service class

We will write simple POJO class which we are going to expose as web-service through Axis2 services, and simple supportive class to help on writing our main service class.

Our main service class (CountryService.java)  will expose its public methods as axis2 web services.



package com.jk.axis2.ws;

public class CountryService {
      
       public Country getCountryDetails(String countryCode) {
             
              System.out.println("countryCode =" + countryCode);
              Country country = null;

              if (countryCode != null) {
                     if (countryCode.equalsIgnoreCase("SL")) {
                           country = new Country("Sri Lanka");
                           country.setCountryCode(countryCode);
                           country.setCity("Colombo");
                           return country;
                     } else if (countryCode.equalsIgnoreCase("FR")) {
                           country = new Country("France");
                           country.setCountryCode(countryCode);
                           country.setCity("Paris");
                           return country;
                     }
              }

              return new Country("NotDefined");
       }
            
       public String getServiceVersion(){
              return "This is initial version 1.0";
       }
      
       public String testEcho(String name){
              return "Testing echo with " + name ;
       }
}





Support Country bean  to provide services through CountryService.


package com.jk.axis2.ws;

public class Country {

       String countryCode =null;
       String city=null;
       String name =null;
      
       public Country(){
             
       }
      
       public Country(String name){
              this.name=name;
       }
      
       public String getCountryCode() {
              return countryCode;
       }
       public void setCountryCode(String countryCode) {
              this.countryCode = countryCode;
       }
       public String getCity() {
              return city;
       }
       public void setCity(String city) {
              this.city = city;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
                    
}




2. Writing  deployment configuration ( services.xml )

To deploy your service archive , you need to have deployment descriptor inside service archive  which tells deployment module to deploy your service as axis2 service.

Basically you can have one or many services inside one single archive file , and can be listed around <ServiceGroup> tag. In this sample we will have only one service inside our service archive.

Our sample services.xml file content.

<serviceGroup>
<service name="CountryService" scope="application">
<messageReceivers>
                                <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
                                                                class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
                                <messageReceiver mep="http://www.w3.org/ns/wsdl/in-out"
                                                                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<parameter locked="false" name="ServiceClass">com.jk.axis2.ws.CountryService</parameter>
</service>
</serviceGroup>


We can define our message receivers inside services.xml  for  each operation separately or as a default for all the services inside services.xml.  We have defined message receivers for all services as .

you can overwrite this as you wish by simply adding messages receiver for specific operation in services.xml as below.

<service>
...
<operation name="testEcho">
<messageReceiver class="org.apache.axis2.receivers. RawXMLINOutMessageReceiver"/> </operation>
</service>


Deploy the sample CountryService.aar  in Axis2



1. Create your service archive file

-Create simple ZIP file and rename as "CountryService.aar"
-Content will be as below




2. Now copy the  CountryService.aar in to the <TOMCAT_HOME>/webapps/axis2/WEB-INF/services   directory

Now your WebService is ready to access.
You will see the CountryService is deployed already.

Try calling service in REST style.

If you need to access as SOAP webservice ,use the WSDL from the path.