Monday, September 24, 2012

Creating basic webservice example

Basic webservice example the manual way using axis2 and testing using SOAP UI

Apache Axis2 is a Web Service engine for deploying the web services, you can find more information about it at its official http://axis.apache.org/axis2/java/core/.
Pre-requisites:

Now unzip and copy axis2.war into Tomcat's webapps directory, start the Tomcat server and navigate to http://localhost:8080/axis2, you should see the below welcome page




For accessing the Administration page you need the credentials admin/axis2 for doing all the necessary administrative activities.
When we click on services link you can see one Version service which dislpays the version which comes by default in the axis2 war bundle.
There are two types of approaches while developing web services, they are:
a) Code first (also called as bottom-up approach): In this approach we will start programming the classes and business logic as java code and then generate the web service contract(wsdl)
b) Contract first (also called as top-bottom approach): In this approach we will start generating class stubs from the WSDL.
We will go with the code first approach which is the most preferred way so let us quickly get into the example
1) Create a project with package and META-INF folders within it as shown below
2) Write the service class first through which we are going to service, let us write a program which validates whether the given number is a palindrome or not PalindromeService.java.
package com.palin;
public class PalindromeService {
  public String isPalindrome(int number) {
    String palin=null;
    try {
      int tempNumber = number;
      int reverse = 0;
      for (int i = 0; i <= number; i++) {
        int temp = number % 10;
        number = number / 10;
        reverse = reverse * 10 + temp;
        i = 0;
      }
      if (tempNumber == reverse) {
        palin="Palindrome";
      } else {
        palin="Not Palindrome!";
      }
    } catch (Exception e) {
      palin="Invalid number!";
    }
    return palin;
  }
}
Create the service descriptor i.e, services.xml (and place in location:WEB-INF)
<service name="PalinServ">
<transports>
    <transport>http</transport>
</transports>
<parameter name="ServiceClass" locked="xsd:false">com.palin.PalindromeService</parameter>
<operation name="isPalindrome">
    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>
The <service> tag is used to define the services, we can even declare the scope for it. We can have multiple services in single services.xml file.
The <transport> tag is used to declare which senders/listeners for SOAP over different protocols like FTP, SMTP, HTTPS, HTTP etc. Here I have used just http. If we are not going to declare any transport, then all the transports that are available in the default axis2.xml file are accepted by default. These can be found in the %TOMCAT_HOME%\webapps\axis2\WEB-INF\conf\axis2.xml with <transportSender> tag.

The <parameter> tag is used to define our service class along with the package name.

The <operation> tag is used to define our method which we are going to expose. All the methods which we wish to publish or do the service are treated as operations in the service descriptor, here isPalindrome is our operation.
RPCMessageReceiver is the messageReceiver which is the most commonly used one for receiving messages.
3) Now we are going to compile the class, create an axis archive file (.aar) as shown below
4) Now copy the PalinServ.aar into webapps/axis2/WEB-INF/services directory and restart your Tomcat and check this url http://localhost:8080/axis2/services/listServices we should be able to see our service along with the default Version service as shown below.
We can see our service name under Service Description section and our method under Available Operations section as shown above.
We can see the .wsdl file url under the Service EPR (endpoint) section but we need to append ?wsdl to its end like http://localhost:8080/axis2/services/PalinServ?wsdl
As the previous version should not get effected, it supports all its previous ones SOAP 1.1, SOAP 1.2 that is the reason why you will be seeing more than one endpoints.

Now, for testing whether you webservice is working or not, see this post http://helptodeveloper.blogspot.com/2012/09/testing-webservice-or-wsdl-with-soap-ui.html.

If you don't have any tools for testing your webservice; you can do manually, look in this post http://helptodeveloper.blogspot.com/2012/09/testing-webservices-manually-with-raw.html on how to test.

No comments:

Post a Comment