This article takes reader step by step to build her/his own simplest program of Java Web Service.
If all steps are followed properly, then the program should work.
SETUP YOUR ENVIRONMENT FIRST.
Assuming that you have:
C:\axis-1_4
C:\Program Files\Apache Software Foundation\Tomcat 5.5
C:\jdk5\lib;
Make sure your AXIS has following JAR files:
C:\axis-1_4\webapps\axis\WEB-INF\lib>dir /b
activation.jar
axis-ant.jar
axis.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
If above highlighted files are missing, then download them separately.
And copy them in C:\axis-1_4\webapps\axis\WEB-INF\lib
NOTE: mail.jar or mailapi.jar should be fine.
set TOMCAT_HOME=C:\Program Files\Apache Software Foundation\Tomcat 5.5
AXIS enable your Tomcat:
Copy “C:\axis-1_4\webapps\axis” folder in %TOMCAT_HOME%\webapps
AXIS itself is an Apache application.
By copying this folder you deploy the AXIS application on Tomcat.
Doing this makes your Tomcat behave like a “Java Web Service” Server.
Verify AXIS Server on Tomcat
Start your Tomcat Server & hit following URL on browser.
http://localhost:8080/axis
You should get Hello! Welcome to Apache-Axis web page.
Click on List to see the list of web services deployed currently.
Set classpath
You need to bring those JAR files in your classpath too.
If not, then will give compilation errors on your command prompt when you try to compile/generate code.
Hence copy all these JAR files to C:\axis-1_4\lib
And set the classpath
C:\>set classpath
classpath=.;c:\jdk5\lib;C:\axis-1_4\lib\axis.jar;C:\axis-1_4\lib\axis-ant.jar;C:
\axis-1_4\lib\commons-discovery-0.2.jar;c:\axis-1_4\lib
YOUR ENVIRONMENT IS NOW SET.
BRACE UP TO WRITE, DEPLOY & EXECUTE THE WEB-SERVICE.
Write your Java Interface
package myapp;
public interface Hello {
public String sayHello(String name);
}
Write your Java Interface Implementation
package myapp;
public class HelloImpl {
public String sayHello(String name) {
return “Hello ” + name;
}
}
So that:
C:\practice\myapp\Hello.java
C:\practice\myapp\HelloImpl.java
Compile interfaces & implementation code before generating WSDL
C:\practice>javac myapp\Hello.java -d .
C:\practice>javac myapp\HelloImpl.java -d .
Generate WSDL
C:\practice>java org.apache.axis.wsdl.Java2WSDL -o hello.wsdl -l”http://localhost:8080/axis/services/hello” -n urn:hello -p”hello” urn:hello myapp.Hello
Notice that hello.wsdl got generated.
Generate glue code
C:\practice>java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p myapp.ws hello.wsdl
So that:
C:\practice\myapp\ws>dir /b
deploy.wsdd
Hello.java
HelloService.java
HelloServiceLocator.java
HelloSoapBindingImpl.java
HelloSoapBindingStub.java
undeploy.wsdd
Time to modify the glue code a bit
Notice “HelloSoapBindingImpl.java“.
It is the implementation that is automatically generated.
But you already have written your own implementation which is: HelloImpl.java
So, call your Impl method in generated Impl method,
Don’t forget to copy your HelloImpl.java in location C:\practice\myapp\ws\
And change its package form myapp to myapp.ws because now it’s been moved there.
/**
* HelloSoapBindingImpl.java
*
* This file was auto-generated from WSDL
* by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter.
*/
package myapp.ws;
public class HelloSoapBindingImpl implements myapp.ws.Hello{
public java.lang.String sayHello(java.lang.String in0) throws java.rmi.RemoteException {
System.out.println(“remote service invoked!”);
HelloImpl h = new HelloImpl();
return h.sayHello(in0);
}
}
Compile the glue code
C:\practice>javac myapp\ws\*.java
May show some warnings but that should be fine.
Package into JAR
C:\practice>jar cvf hello.jar myapp/*.class myapp/ws/*.class
Move hello.jar
C:\practice>move hello.jar “c:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis\WEB-INF\lib
Start Tomcat server before deploying
Deploy the service
C:\practice>java org.apache.axis.client.AdminClient myapp\ws\deploy.wsdd
Verify deployed service
Re-Start your Tomcat Server & hit following URL on browser.
http://localhost:8080/axis
You should get Hello! Welcome to Apache-Axis web page.
Click on List to see the list of web services deployed currently containing your hello service.
Write the Client
package myapp.client;
public class HelloServiceClient {
public static void main(String[] args){
try {
//Get the service.
myapp.ws.HelloService service = new myapp.ws.HelloServiceLocator();
// Get the stub.
myapp.ws.Hello hello = service.gethello();
// Make the call.
System.out.println(hello.sayHello(“some name”));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Compile the client
javac myapp\*.java -d .
Execute the client
C:\practice>java myapp.client.HelloServiceClient
Output should be:
Hello some name
Recommended references:
http://www.onjava.com/lpt/a/1578
http://www.ociweb.com/mark/JavaUserGroup/Axis.pdf
http://jaitechwriteups.blogspot.com/2006/12/simple-webservice-on-jboss-using-axis.html