Thursday, March 17, 2011

Axis2 Webservice Hello World Tutorial

For this simple Hello World tutorial, you don’t have to install Tomcat and IDE. You can just use DOS command in Windows.

1. Install JDK 6(SE 6) from http://www.oracle.com/technetwork/java/javase/downloads/index.html
2. Download Ant from http://ant.apache.org/bindownload.cgi and install it
3. Download Axis2 from http://axis.apache.org/axis2/java/core/download.cgi
4. Unzip Axis2 into your C:\tools\axis2-154\axis2-1.5.4\
5. Create folder c:\helloworld
6. Create folder c:\helloworld\client
7. Create folder c:\helloworld\client\code
8. Create folder c:\helloworld\server
9. Create folder c:\helloword\server\META-INF
10. Now, create server java file – SayHello.java and save it in c:\helloworld\server
public class SayHello {
// This is service.
public String sayHello(String name) {
return "Hello " + name;
}
}
11. Compile it:







12. Create services.xml file:
<service>
<parameter name="ServiceClass"
locked="false">SayHello</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</operation>
</service>

13. Save services.xml into c:\helloword\cleint\META-INF
14. Create sayhello.aar file:










15. Go to http://www.eatj.com/ to create a Free Trial account.
16. Use FTP upload your sayhello.aar file into /webapps/axis2/WEB-INF/services (For ftp settings, login to your EATJ account and click on Settings)
17. Go to http://www.eatj.com/ and login to your account and click on RESTART link.
18. After restarting, you will see a URL: http://..eatj.com/ . Your Axis2 URL will be http://..eatj.com/axis2/
19. Click on Services link. You should see your service: sayhello
20. Click on sayhello. You should see WSLD. URL is http://..eatj.com/axis2/services/sayhello?wsdl
21. So far, you created your first web service.
22. Now, we create client code to access this sayhello service.
23. Create client stub. Type: C:\tools\axis2-154\axis2-1.5.4\bin\wSDL2Java.bat -uri http://..eatj.com/axis2/services/sayhello?wsdl -o code/



24. Create client code – SayHelloClient.java and save it into C:\helloworld\client\code\src\org\apache\ws\axis2
package org.apache.ws.axis2;

import org.apache.ws.axis2.SayhelloStub.SayHelloResponse;

public class SayHelloClient {
public static void main(String[] args) throws Exception {
SayhelloStub stub = new SayhelloStub();

//Create the request
SayhelloStub.SayHello request = new SayhelloStub.SayHello();
request.setName("John");

//Invoke the service
SayHelloResponse response = stub.sayHello(request);
System.out.println("Response : " + response.get_return());
}
}
25. Go to C:\helloworld\client\code and type: ant
26. Your all classes is in C:\helloworld\client\code\build\classes now.
27. Go to C:\helloworld\client\code
28. Type: java -classpath C:\helloworld\client\code\build\classes;C:\tools\axis2-154\axis2-1.5.4\lib\* org.apache.ws.axis2.SayHelloClient
29. You should get: Response : Hello John

More tutorials: http://jsptutorblog.blogspot.com/