1. View: Take a peek at the existing code
There are two files of our existing code: an interface and an implementation class.
package fibonacci;
public interface Fibonacci {
// Method to calculate the fibonacci sequence
public int calculateFibonacci( int num );
// Method to return an array of results
public int[] calculateFibonacciRange(int start, int stop);
}
Then we have the real implementation of that code.
package fibonacci;
public class FibonacciImpl {
public int calculateFibonacci( int num ) {
//----- implementation here -----
}
public int[] calculateFibonacciRange(int start, int stop) {
//----- implementation here -----
}
}
2. Java2WSDL: Generate the WSDL file for the given Interface
The Java2WSDL command line will generate a standard WSDL file that conforms to a given interface.
java org.apache.axis.wsdl.Java2WSDL -o fib.wsdl -l "http://localhost : 8080/axis/services/fibonacci" -n urn:fibonacci -p "fibonacci"urn:fibonacci fibonacci.Fibonacci
After the program runs, we see that a new file, fib.wsdl, was created for us.
Now we have defined our Web service.
3. WSDL2Java: Generate the Server-side Wrapper code and Stubs
Take this WSDL and generate all of the glue code for deploying the service, as well as stubs for accessing it.
Let's generate this code into the fibonacci.ws package, to keep it separate from the original code.
java org.apache.axis.wsdl.WSDL2Java -o . -d Session -s -p fibonacci.ws fib.wsdl
After running this program, a slew of code has been generated for us in the fibonacci \ ws directory :
* FibonacciSoapBindingImpl.java : This is the implementation code for our Web service. This is the one file we will need to edit, tying it to our existing FibonacciImpl.
* Fibonacci.java : This is a remote interface to the Fibonacci system (extends Remote, and methods from original Fibonacci.java throw RemoteExceptions).
* FibonacciService.java : Service interface of the Web services. The ServiceLocator implements this interface.
* FibonacciServiceLocator.java : Helper factory for retrieving a handle to the service.
* FibonacciSoapBindingSkeleton.java : Server-side skeleton code.
* FibonacciSoapBindingStub.java : Client-side stub code that encapsulates client access.
* deploy.wsdd : Deployment descriptor that we pass to the Axis system to deploy these Web services.
* undeploy.wsdd : Deployment descriptor that will undeploy the Web services from the Axis system.
4. Fill-in Wrapper to Call the existing Fibonacci code (FibonacciSoapBindingImpl)
We need to tweak one of the output source files to tie the Web service to FibonacciImpl.java.
FibonacciSoapBindingImpl.java is waiting for us to add the stuff into the methods that it created.
We are simply tying in to the existing class. We could have hard-coded the methods in this class, but in the real world, we probably want to wrap logic as Web services, and not just enable access via that interface.
2 ways toinclude actual business logic in Binding implementation class :
a. Copy / Paste the existing code to new generated binding class.
b. Write new code in binding class.
5. Deploy : Deploy the service to Apache Axis
a. Compile the Service Code (Noneed to compile in IDE)
b. Package the code for Axis to find and copy it into Axis' classpath
jar cvf fib.jar fibonacci/*.class fibonacci/ws/*.class
mv fib.jar %TOMCAT_HOME%/webapps/axis/WEB-INF/lib
c. Deploy the Web Service using the WSDD Deployment Descriptor :
java org.apache.axis.client.AdminClient /..../deploy.wsdd
<admin>Done processing</admin>
d. Restart the server
Now our Fibonacci Web service is alive and running in the server !
Check the WSDL deployment using : http://localhost:8080/axis
Then click list and then choose your web service.
Create a Web Service Client to call Web Service
1. Set the classpath (Following JARsare needed to be set in class-path):
setCLASSPATH=activation.jar;mailapi.jar;axis-ant.jar;axis.jar;log4j-1.2.8.jar;saaj.jar;wsdl4j-1.5.1.jar;commons-discovery-0.2.jar;commons-logging-1.0.4.jar;jaxrpc.jar;
2. Generate the Java classes from wsdl file
java org.apache.axis.wsdl.WSDL2Java fib.wsdl
It will make all the data classes / POJOs and Web Service specific classes in the same place where the wsdl file stored.
3. Write code in main method of class
// Make a service
fibonacci.ws.FibonacciService service = new fibonacci.ws.FibonacciServiceLocator();
// Now use the service to get a stub to the service
fibonacci.ws.Fibonacci fib = service.getFibonacci();
// Make the actual call
System.out.println("Fibonacci(10) = " + fib.calculateFibonacci(10));
No comments:
Post a Comment
Note: only a member of this blog may post a comment.