A Web Service is:
- A programmable application, accessible as a component via standard Web protocols,
- Uses standard Web protocols like HTTP, XML and SOAP,
- Works through existing proxies and firewalls,
- Can take advantage of HTTP authentication,
- Easy incorporation with existing XML messaging solutions. Takes advantage of XML messaging schemas and easy transition from XML RPC solutions,
- Available to a variety of clients (platform independent).
To start with, we would need the following JAR Files:
activation.jar |
axis.jar |
axis-ant.jar |
commons-discovery-0.2.jar |
commons-logging-1.0.4.jar |
jaxrpc.jar |
log4j-1.2.8.jar |
mail.jar |
saaj.jar |
wsdl4j-1.5.1.jar |
xercesImpl.jar |
xml-apis.jar |
The dependencies of AXIS Framework are listed in http://ws.apache.org/scout/dependencies.html
We dont need to download the individual JAR files. Just download the AXIS framework from http://ws.apache.org/axis/
All the dependencies JAR files would also be included in the ZIP File. Unzip this ZIP File and place it inside your Eclipse Workspace.
Setup the Apache Tomcat server. If you havent done this before, read this manual to install and configure the server.
http://www.coreservlets.com/Apache-Tomcat-Tutorial/Preconfigured-Tomcat-Version.html
Install, Configure and start the server using the steps given in the above link.
Now, download the WAR file for AXIS framework from
http://ws.apache.org/axis2/download/1_3/download.cgi
Now we need to restart the Apache Tomcat server. Once the server is restarted we would notice that a new folder axis2 would be created in the webapps folder. We would store our Java Class files for the webservices within this folder.
Check if the AXIS framework is correctly installed or not by entering
http://localhost/axis2/services/listServices in the web browser. the output should be like the below
Now download two Eclipse Plugins for this AXIS Framework. They are:
1. Service Archive Wizard - Eclipse Plug-in
2. Code Generator Wizard - Eclipse Plug-in
They can be downloaded from
http://ws.apache.org/axis2/tools/index.html
Download these ZIP files and extract them to the plugins folder of ECLIPSE. Restart ECLIPSE with -clean option to install these plugins.
Now create two JAVA Files in ECLIPSE which will be define the web services .
TestWebserviceDef.java
-----------------------------
package testWebservice;
public interface TestWebserviceDef {
//Method that will be exposed as service. this contains only definitions
public String hostedService( int num );
}
TestWebserviceImpl.java
-----------------------------
package testWebservice;
public class TestWebserviceImpl implements TestWebserviceDef {
public String hostedService( int num )
{
return ("Hi, Webservice called with parameter "+num);
}
}
Now generate a WSDL file using the Eclipse Plugins for AXIS.
Go to New->Other->Axis2 Wizards as shown below and Click on "Axis2 Code Generator"
Select the Option "Generate WSDL from a Java Source File"
Now Define the Fully Qualified Class of the Web Service whose WSDL we are going to create. Remember: The class should be in path of the folder/jar that we add the Java Class Path Entries as shown here-
Click on Test Class Loading. If it says successful, then finish button would be enabled. Press this button and move forward.
Now, set the options as shown in the snapshot. The TargetNameSpace should ideally be same as your package name in which you are hosting the Web Service Java Classes.
The Service Name is the name of the class name which will be hosted as a web service. Others use the same value as shown here in the snapshot.
Now, Create a folder WSDL in the Eclipse project workspace and browse this location to "Output Location". By default you will get services.wsdl in the output file name.
This is the last screen. Click Finish to complete the Wizard.
You will see a message box "All operations completed successfully".
The WSDL would be generated in the location specified here. You can open the services.wsdl in Notepad to see how a WSDL looks like.
Now Generate the Service Archive using the other AXIS plugin - "Axis2 Service Archiver". CLick New -> Other -> Axis2 Wizard -> Axis2 Service Archiver
Point to the location where the Class files for the two Java Files are located. Normally in ECLIPSE they would be automatically built, and so we would have to point to the bin folder corresponding to our Project Workspace folder.
If you dont have any other JAR file, then click on "Include .class files only". It will select the class files located in this path.
Select the WSDL generated above by pointing to the full path where the WSDL is located.
In the next dialog box, it will ask if you need to select a "service.xml". CLick on the check box "Generate the service xml automatically"
Now this is the key part. Now we need to generate the Service Archiver. This needs to be placed inside
%TOM_CAT_SERVER_PATH%\ WEBAPPS\axis2\WEB-INF\services
where
%TOM_CAT_SERVER_PATH% represents the path for Apache Tomcat server installation.
In my example it is
C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.10\webapps\axis2\WEB-INF\services
The Name of the Output File Name should be the same as the Java Class which is going to be exposed as Web Service. Click FInish to generate TestWebserviceImpl.aar in the folder
C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.10\webapps\axis2\WEB-INF\services
Now we need to generate the STUB files:
Again we go to Axis2 Code Generator. However this time we will chose a different option.
Select the option "Generate Java Source Code from a WSDL File"
Specify the WSDL File's full path and location in the next screen and press Next.
Now here is another Key part. Be careful while filling this part.
Imp: The Custom Package Name should be the same as the package name where the Java codes where stored. Else a lot of compilation errors would come in Stubs.
Prefer the Codegen option to be default if you are doing it for the first time.
Now select the option "Browse and select a project on current eclipse workspace" and check the option "Add the Axis2 codegen jars to the codegen resulted project".
Give the location of the project workspace. (Eg.
C:\Documents and Settings\The Geek House\workspace\MyfirstWebService
Press Finish.
A new folder lib gets generated with lot of JAR's in the project workspace. We need to include these JAR's in the project External JAR list. Also, two files are also generated - TestWebserviceImplCallbackHandler.java and TestWebserviceImplStub.java. Include these files in Eclipse Workspace and compile them after including JAR files generated above.
Now we will write the client to consume this web service.
testWebserviceClient.java
----------------------------
package testWebservice;
import testWebservice.TestWebserviceImplStub;
public class testWebserviceClient {
/**
* @Arvind K R
*/
public static void main(String[] args) {
TestWebserviceImplStub stub;
try {
stub = new testWebservice.TestWebserviceImplStub("http://localhost/axis2/services/TestWebserviceImpl");
TestWebserviceImplStub.HostedService Service1 = new TestWebserviceImplStub.HostedService();
Service1.setNum(10);
TestWebserviceImplStub.HostedServiceResponse Response1 = stub.hostedService(Service1);
String myResponse = Response1.get_return();
System.out.println("Response" + myResponse);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Now run this Java Class in Eclipse. The Output is :
- Deploying module: addressing-1.3
Responsewebservice called with parameter 10
Voila!! Your web service is now configured and ready to run !!
Important thing:
Try to keep the JDK version used by Apache TOMCAT Server and ECLIPSE same. It will prevent lot of errors while running the client java class. Ideally the JDK version used by Eclipse should be lesser than or same as the JDK version used by Apache Tomcat. This is because Sun allows backward compatibility of the JDK.
This is one major reason for this error - java.lang.UnsupportedClassVersionError: Bad version number in .class file
References:
http://ws.apache.org/axis/
http://ws.apache.org/axis2/1_3/installationguide.html
http://wso2.org/library/1719
0 comments :
Post a Comment