Introduction

There are numerous ways one can make a Web Service , some of them are listed below :
According to me best choice would be Netbeans axis2 support as I haven't found any bug in it yet .
Eclipse axis2 support lets you know what exactly one is doing but it has bugs which are still open and has not been fixed yet. Hoping that in future these bugs would be removed :) .
This blog provides a detailed tutorial for creating a Web Service using Axis2 support in Eclipse. I will split the tutorial into sections for easy reference.
Sections :

Tools Required

I will list the tools require for creating a web service in a easy way. Best part is all tools are open source :) so no money is required. Though all the tools listed below are not require but for easy development one should use these tools .
Here's the list
If any of the above link is broken , then please google for other links , these softwares are free and they will surely be available for download.
Notes:
  1. Axis2 War can be created using Axis2 binary distribution also
  2. CodeGen Axis Plugin is for easy development and work can be done without it .But I will explain using this tool. These tools are inbuilt in Axis2 binary distribution so one can use command line tools.

Setting up environment for development

This is the most time consuming section , specially for a new user , I will try to reduce your time as much as possible.Ok lets start then :
I am assuming you know about Eclipse if not then do explore about it , a developer should know it :).

JDK Installation
  • First of all install JDK into the system. Please install this one correctly as all other tools will use this . After installing check in computer properties , environment variables list that following parameters are there :
JAVA_HOME: Directory path where u install JDK
Path : JAVA_HOME/bin folder of JDK
Example
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_17
PATH=C:\Program Files\Java\jdk1.6.0_17\bin\
I believe you need to set the JAVA_HOME variable yourself.
To know how to set environment variables see here

Eclipse Installation
  • Now lets install Eclipse into your system. Just extract the zipped files into your harddrive and installation done.
  • Now lets install the the two plugins(last two tools in the list ) we downloaded . Just go to plugins folder of Eclipse and extract the zipped file of plugin there.
  • Lets see if plugins are installed correctly .
Run eclipse.exe
Open New->other->Axis2 , if this option is present then plugins installed correctly

Axis2Binary Installation
  • Unzip the downloaded zipped file into desired location
  • This step is optional and perform only if you want to use inbuilt tools of axis .
Set a environment variable with name as AXIS_HOME= directory url where u unzipped the axis binary files
example
AXIS_HOME=C:\axis2

TomCat Server
  • Just run the setup file included in the zipped folder
  • Go to bin folder and start server
  • Visit link http://localhost:8080 to check the installation
Configuring Axis 2 with Tomcat Server
  • Copy the Axis 2 WAR distribution file into the webapp folder of tomcat home directory
  • Restart the Tomcat Server
  • In browser visit http://localhost:8080 /axis2 and you should see axis2 home page if everything is installed correctly
  • Click on validate installation to Validate your axis2 integration
Note: If you want to create the war file from axis2 binary distribution then follow following steps
Go to axis to binary distribution folder search for axis-web folder
Build.xml file will be present there
Open Build.xml in Eclipse.exe
Click on Run button in eclipse
The war file will be created in axis2 binary home directory/dist folder


Now we are all ready to start Coding :).

Writing Java Service Class

This section gives a general method to write a Java Class for any Web Service. This method is adopted by me and to be sure I dont know if its the best method , anyway everybody is free to use their style of coding the class.
The JavaToWSDL tool does not care how you are getting the work done , it just concerns about the datatypes your service is returning and the datatypes its taking as input.
Each datatype used for returning or taken as input by the service should be a JavaBean if the datatype is custom made class , default types of java are off coarse allowed :).
We will be using Eclipse and We will create a package let name it webservice1
  • First create a new Java Project let name the project LearningWebService in Eclipse .
  • Second, Create a DataBean class in the project which we will use as return type . One can put any datatypes in databean and then send the whole databean via service . The databean code is as follows
package webservice1 ;
import java.io.Serializable;

public class DataBean implements Serializable {
/*Put the variables which you want to
send here */
String name;
String[] sarray;
public DataBean() {}

public String getName()
{
return name;
}
public String getSarray()
{
return sarray;
}
public String setName(String name)
{
this.name=name;
}
public void setSarray(String[] sarray)
{
this.sarray=sarray;
}

}
  • Now we will write the Java service class.This should be in seperate TestService.java file
package webservice1 ;
public class TestService
{
public DataBean serviceFunction(String arg1,String arg2)
// Assuming we have two input arguments
{
/*do processing with input parameters using
any java classes u want.
Let say we get a result after processing*/

String[] resultarray={"Test"," Success"};
String name="Chirag";
DataBean data=new DataBean();
data.setName(name);
data.setSarray(resultarray);
return data;

}
}

  • Now create a dummy class containing a main function just to compile the above classes formed. Dummy class is as follow.
public class dummy
{
public static void main(String[] argv)
{
TestService temp=new TestService();
return;
}
}

I believe we got the .class file in the respective folder in project directory.
Now lets move to next step of creating WSDL out of this Java class file.

Creating WSDL (using JavaToWSDL tool)


Important Note :
To use this tool we need minimum JDK version 6 and we need to open Eclipse with clean parameter (as explained below)

  • Open Eclipse from command line using syntax given below
EclipseHomeDirectoryPath\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M
Example:
C:\Eclipse3311\eclipse\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M

  • Now Select the New->Other->Axis2 wizards->Axis2 CodeGenerator
  • Select Generate a WSDL from Java Source File and Click on next
  • Type the class name as in our case it is webservice1.TestService
  • Select the folder containing the TestService.class file
  • Now test if class is loadable by clicking on TEST CLASS LOADING ,if class doesnt loads that means wrong folder is added
  • Click on next button , next button will appear only if class loads successfully
  • Set the options as per your choice , as for us we will set as follow :
TargetNameSpace : webservice1
TargetNameSpace Prefix : misty
SchemaTargetNameSpace :webservice1/types
SchemaTargetNameSpace Prefix : types
ServiceName : TestService

  • Click on next button
  • Select the location where you want to place the output WSDL file , I recommend select src folder in the project workspace created by eclipse
  • Click on finish
A message should pop up acknowledging the success

Now after getting the WSDL file we will generate the client stubs next.

Creating Java Client Stubs (using WSDLtoJava tool)

Important Note :
To use this tool we need minimum JDK version 6 and we need to open Eclipse with clean parameter (as explained below)

  • Open Eclipse from command line using syntax given below
EclipseHomeDirectoryPath\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M
Example:
C:\Eclipse3311\eclipse\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M

  • Now Select the New->Other->Axis2 wizards->Axis2 CodeGenerator
  • Select Generate Java Source Code from WSDL File and Click on next
  • Browse the WSDL file we created just now and click on next
  • Click on next again
  • Select the output file folder, in our case select the webservice1 folder in project folder
  • Tick the Add the Axis2 codegen jars to the codegen resulted project option
  • Tick the Add Axis2 libraries to the codegen result project
  • Give the path of the Axis2 binary home directory , this is to get all the libraries(jars) used by stub into the lib directory in project folder
  • Click on finish
  • A message should pop up acknowledging the success
  • Now If you have given the path for your Axis home correctly then you should get the lib folder in your project folder of eclipse and it will contain all the required jars in it.
  • You need to add the these jars into your project properties. I hope you know how to add libraries to your project in Eclipse , if not please see web for it , this is not a tutorial on Eclipse.
  • If you correctly included the libraries then the red lines in the generated stubs files in the project should go :) and they should be error free.

Writing Java Client ( General )

Now we will create a java class which will call our service using the stubs produced by the tool in last section.
We created the stubs from the WSDL file and WSDL from our TestService java class.
Lets remind some terms from TestService java class
ClassName : TestService
Function Name : serviceFunction
Input Parameters : arg1,arg2
Return Type : DataBean

Lets first see how the client code will look like then I will explain it line by line :

package webservice1;

public class TestServiceClient {
public static void main(String[] args) {
TestServiceStub stub;
try {
stub = new TestServiceStub
("http://localhost:8080/axis2/services/TestService");
TestServiceStub.ServiceFunction obj = new TestServiceStub.ServiceFunction();
obj.setArg1("argument1");
obj.setArg2("argument2");
TestServiceStub.ServiceFunctionResponse res = stub.serviceFunction(obj);
TestServiceStub.DataBean data=new TestServiceStub.DataBean();
data=res.get_return();
String[] dataarray=data.getSarray();
System.out.println(data.getName()+dataarray[0]+dataarray[1]);
} catch (Exception e) {
e.printStackTrace();
}
}
}

  1. First we create the stub object of TestServiceStub , our class name was TestService hence TestServiceStub, thus to generalize it would be ClassNameStub
  2. Second We initialize the stub and give the service URL as parameter to constructor , in our example it will be as shown only , in general it would be "http://serverroot/axis2/services/Servicename"
  3. Then we create the object for our function ServiceFunction ,with syntax as shown in general it would be ClassNameStub.functionname(with first letter capital ) obj=new ClassNameStub.functionname(with first letter capital )()
  4. Then we set the input arguments , in general they can be set as obj.setInputargumentname(with first letter capital)(argument value)
  5. After setting all input arguments we create the response object using syntax as shown above , in general it would be ClassNameStub.Functionname(with first letter as capital) Response res = stub.Functionname(obj);
  6. After this we created the Databean object, in general this step wont come for default types , for custom types syntax would be ClassNameStub.DatatypeClassname data=new ClassNameStub.DataTypeClassName();
  7. After this we got the result into the databean using res.get_return() function
  8. After getting the result one can do anything with it
So general client code would look like as below (You need to change the blue text according to your class specifications)
package pacakagename;

public class ClassNameClient {
public static void main(String[] args) {
ClassNameStub stub;
try {
stub = new ClassNameStub
("http://serverroot/axis2/services/Servicename");
ClassNameStub.Functionname(with first letter as capital) obj = new ClassNameStub.Functionname(with first letter as capital)();
obj.setInputArgumentName(with first letter as capital)(argument1);
ClassNameStub.Functionname(with first letter as capital) Response res = stub.Functionname(obj);
ClassNameStub.DatatypeClassname data=new ClassNameStub.DataTypeClassName();
data=res.get_return();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Creating .aar file of service

Important Note : There is a bug in service archiver 1.3 version , and its still(as on 4th june 2010) open issue , there is workaround for this bug which is to do some deletion before starting Eclipse , read below for proper steps :

We will use Axis2 service archiver tool for this.
  • Go to your workspace folder which Eclipse creates , You will see a metadata folder there , go to metadata>plugins>servicearchiver>, and delete the file present in it.[if you are opening Eclipse for the first time then no need for this step]
  • Open Eclipse from command line using syntax given below
EclipseHomeDirectoryPath\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M
Example:
C:\Eclipse3311\eclipse\eclipse.exe -clean -console -consoleLog -debug -vmargs -Xmx384M

  • Now Select the New->Other->Axis2 wizards->Axis2 Service Archiver
  • Browse the location of the class file TestService.class we generated in second section , it must be present in project folder ->bin folder.
  • Click on next
  • Browse WSDL file we created and click on next
  • In general if your class depends on some libraries then include those libraries now , in our case we dont need any other library so click on next
  • If you have custom services.xml file then browse it here otherwise , for our example we will create services.xml file automatically so select Generate services.xml automatically click on next .
  • Now give the service name and class name as follow
ServiceName : TestService
Class Name : webservice1.TestService
  • Click on load , if filepath was given correctly then class should low and u should see serviceFunction() in the table
  • Click on next
  • Browse the output location, select Project folder(or any folder of your choice)
  • Give file name as TestService
  • Click on finish
You will get two files TestService.aar and build.xml in the location you selected.

Uploading the service

  • Start the tomcat server
  • Go to url http://localhost:8080/axis2
  • Click on Administation link
  • Default credentials for admin login for axis 2 is as follows
Username : admin
Password : axis2
  • Select upload a service from left panel
  • Upload the TestService.aar file we created in last section
  • Click on upload
  • Now to validate the upload go to url http://localhost:8080/axis2
  • Click on Services Link
  • TestService should be listed their now.
Note : This method has some problem when the server is Weblogic server but works perfectly fine with Tomcat Server

Testing the service

  • Now to test the service , we will run the java client class we created earlier.
  • Run the java client class in eclipse
  • In console window , we should see the output of System.out.print , as in our case result should be
ChiragTestSuccess

We created the our first Web Service , Such a easy job Isn't it? :)