Wednesday, February 17, 2010

WebServices

POJO WebService & ADB Client
1) Write POJO class with required business logic.
2) Create AAR file & copy it into "services" folder of AXIS2.war
3) Start server & create ADB client using following command
WSDL2Java -p org.sample.stub -uri http://localhost:8080/axis2/services/BankingService?wsdl
4) Create Client class & access webservice using WebServiceClient stub created in step 3.

POJO Class:-

package org.sample.service;

import org.sample.util.EmployeeInfo;

public class BankingService {

public EmployeeInfo employeeDetails(String empno)
{
EmployeeInfo empInfo = new EmployeeInfo();

empInfo.setName("ABC");
empInfo.setEmployeeID(1111111);

return empInfo;
}

}


package org.sample.util;

public class EmployeeInfo{

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

CLIENT Class
package org.sample.stub;

import java.rmi.RemoteException;
import org.sample.stub.BankingServiceStub.EmployeeInfo;

public class Client {

public static void main(String[] args)
{
try {

String URL = "http://localhost:8080/axis2/services/BankingService";

BankingServiceStub stub = new BankingServiceStub(URL);

BankingServiceStub.EmployeeDetails empDetails = new BankingServiceStub.EmployeeDetails();

empDetails.setEmpno("a");

BankingServiceStub.EmployeeDetailsResponse empDtl = stub.employeeDetails(empDetails);

EmployeeInfo empInfo = (EmployeeInfo)empDtl.local_return;

System.out.println("Emp Id " + empInfo.getEmployeeID());
} catch (AxisFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

RPCClient
public static void main(String[] args1) throws AxisFault {

RPCServiceClient serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

EndpointReference targetEPR
= new EndpointReference(
"http://localhost:8080/axis2/services/BankingService");

options.setTo(targetEPR);

// Get the weather (no setting, the Spring Framework has already initialized it for us)
QName opGetResult = new QName("http://service.sample.org", "employeeDetails");

String name = "abc";

Object[] opGetResultArgs = new Object[] {name};
Class[] returnTypes = new Class[] { EmployeeInfo.class };

Object[] response = serviceClient.invokeBlocking(opGetResult,
opGetResultArgs, returnTypes);

EmployeeInfo result = (EmployeeInfo) response[0];

// display results
if (result == null) {
System.out.println("No data found!");
}else{
System.out.println("Emp Record " + result.getName());
}
}

}

No comments:

Post a Comment