Saturday, June 13, 2009

WebService using Apache axis

Download the following jar

axis.jar,axis-ant.jar,commons-discovery-0.2.jar,commons-logging-1.0.4.jar,jaxrpc.jar,
log4j-1.2.8.jar,saaj.jar,wsdl4j-1.5.1.jar.
Create one project eg: CalcTest and set classpath to theses jars.

Download the axis.war file from http://ws.apache.org/axis/java/install.html and put into your tomcat(webapps) or jboss(deploy) folder.

Create one interface

package com.jijo;
/**
* @author jijo
*
*/
public interface Calculator {
public int add(int x,int y);
public int subtract(int x, int y);
}

Create one class and implemet the Calculator interface
package com.jijo;
public class CalculatorImpl implements Calculator {
public int add(int x, int y) {
return x+y;
}
public int subtract(int x, int y) {
return x-y;
}
}
Compile the classes, and in command prompt:
java org.apache.axis.wsdl.Java2WSDL -o calculator.wsdl -n urn:com.jijo.Calculator -l http://localhost:8080/axis/services/calcservice com.jijo.Calculator
[This is for creating the WSDL(Java to WSDL)].

After this a wsdl file is created in you folder.
Then Create the Stubs based on this WSDL(WSDL to Java)
java org.apache.axis.wsdl.WSDL2Java -o ..\src -p com.jijo.stubs -s calculator.wsdl
Then the stubs is created .

Replace the
CalcserviceSoapBindingImpl.java to the following


/** * CalcserviceSoapBindingImpl.java * * This file was auto-generated from WSDL * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter. */
package com.jijo.stubs;
import com.jijo.CalculatorImpl;

public class CalcserviceSoapBindingImpl implements com.jijo.stubs.Calculator{ com.jijo.Calculator calculator = new CalculatorImpl(); public int add(int in0, int in1) throws java.rmi.RemoteException { return calculator.add(in0, in1); }
public int subtract(int in0, int in1) throws java.rmi.RemoteException { return calculator.subtract(in0, in1); }
}


create a jar eg: cal.jar based on the classes and put into axis.war/WEB-INF/lib folder
start the tomcat/jboss server.Then
Deploy into axis.war using ,
java org.apache.axis.client.AdminClient ..\src\com\jijo\stubs\deploy.wsdd

One server-config.wsdd file is created in the axis.war folder.
Then restart the server.
Check the wsdl file getting using,in browser
http://localhost:8080/axis/services/calcservice?wsdl

Create a client class

import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import com.jijo.stubs.CalculatorService;
import com.jijo.stubs.CalculatorServiceLocator;

public class CalcClient {
public static void main(String[] args) {
CalculatorService service = new CalculatorServiceLocator();
try {
int result = service.getcalcservice().add(10, 20);
System.out.println("Result :"+result);
result = service.getcalcservice().subtract(10, 20);
System.out.println("Result:"+result);
} catch (RemoteException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
}

No comments: