Monday, February 15, 2010

Web Service Complete Program

--------------------------------------------------------------------------------------
Server Side Code
--------------------------------------------------------------------------------------

package home.object;

public class Address {
private String street;
private String city;
private Integer pin;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Integer getPin() {
return pin;
}
public void setPin(Integer pin) {
this.pin = pin;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
}
--------------------------------------------------------------------------------------
package home.object;

public class Employee {
private String name;
private Integer empNo;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Integer getEmpNo() {
return empNo;
}
public void setEmpNo(Integer empNo) {
this.empNo = empNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------------------------------------------------------------------------------
package home.webservice;

import home.object.Employee;

/**
* @author Soumya Sree
*
*/
public interface EmployeeInterface {
public void displayEmp(Employee emp);
}
-------------------------------------------------------------------------------------
package home.webservice;

import home.object.Address;
import home.object.Employee;

import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import org.jboss.wsf.spi.annotation.WebContext;
@WebService(name = "EmployeeWebService", targetNamespace = "http://object.home/")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebContext(contextRoot = "/empoperations", urlPattern = "/displayEmp")
@Stateless()
public class EmployeeWebService implements EmployeeInterface{
@WebMethod(operationName = "displayEmp", action = "displayEmp")
public void displayEmp(@WebParam(name = "Employee", targetNamespace = "http://object.home/", partName = "employee") Employee emp) {
System.out.println("Employee Name : " + emp.getName());
System.out.println("Employee Number : " + emp.getEmpNo());
Address address = emp.getAddress();
System.out.println("Street : " + address.getStreet());
System.out.println("City : " + address.getCity());
System.out.println("PIN : " + address.getPin());
}
}
-------------------------------------------------------------------------------------

Create the Jar with the help of above files and deploy it into application server, the server automatically creates the WSDL (Web Service Description Language) file, with the help of wsdl and wsdl2java.jar we can write the client side code.
Generated wsdl is looks like the below content.
--------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------
Client Side Code (Generated with the help of wsdl2java.jar file)
--------------------------------------------------------------------------------------
/**
* Address.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public class Address implements java.io.Serializable {
private java.lang.String city;
private java.lang.Integer pin;
private java.lang.String street;

public Address() {
}

public java.lang.String getCity() {
return city;
}

public void setCity(java.lang.String city) {
this.city = city;
}

public java.lang.Integer getPin() {
return pin;
}

public void setPin(java.lang.Integer pin) {
this.pin = pin;
}

public java.lang.String getStreet() {
return street;
}

public void setStreet(java.lang.String street) {
this.street = street;
}

private java.lang.Object __equalsCalc = null;
public synchronized boolean equals(java.lang.Object obj) {
if (!(obj instanceof Address)) return false;
Address other = (Address) obj;
if (obj == null) return false;
if (this == obj) return true;
if (__equalsCalc != null) {
return (__equalsCalc == obj);
}
__equalsCalc = obj;
boolean _equals;
_equals = true &&
((this.city==null && other.getCity()==null) ||
(this.city!=null &&
this.city.equals(other.getCity()))) &&
((this.pin==null && other.getPin()==null) ||
(this.pin!=null &&
this.pin.equals(other.getPin()))) &&
((this.street==null && other.getStreet()==null) ||
(this.street!=null &&
this.street.equals(other.getStreet())));
__equalsCalc = null;
return _equals;
}

private boolean __hashCodeCalc = false;
public synchronized int hashCode() {
if (__hashCodeCalc) {
return 0;
}
__hashCodeCalc = true;
int _hashCode = 1;
if (getCity() != null) {
_hashCode += getCity().hashCode();
}
if (getPin() != null) {
_hashCode += getPin().hashCode();
}
if (getStreet() != null) {
_hashCode += getStreet().hashCode();
}
__hashCodeCalc = false;
return _hashCode;
}

// Type metadata
private static org.apache.axis.description.TypeDesc typeDesc =
new org.apache.axis.description.TypeDesc(Address.class);

static {
typeDesc.setXmlType(new javax.xml.namespace.QName("http://object.home/", "address"));
org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("city");
elemField.setXmlName(new javax.xml.namespace.QName("", "city"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("pin");
elemField.setXmlName(new javax.xml.namespace.QName("", "pin"));
elemField.setXmlType(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/encoding/", "int"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("street");
elemField.setXmlName(new javax.xml.namespace.QName("", "street"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
}

/**
* Return type metadata object
*/
public static org.apache.axis.description.TypeDesc getTypeDesc() {
return typeDesc;
}

/**
* Get Custom Serializer
*/
public static org.apache.axis.encoding.Serializer getSerializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanSerializer(
_javaType, _xmlType, typeDesc);
}

/**
* Get Custom Deserializer
*/
public static org.apache.axis.encoding.Deserializer getDeserializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanDeserializer(
_javaType, _xmlType, typeDesc);
}

}
--------------------------------------------------------------------------------------
/**
* Employee.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public class Employee implements java.io.Serializable {
private home.object.Address address;
private java.lang.Integer empNo;
private java.lang.String name;

public Employee() {
}

public home.object.Address getAddress() {
return address;
}

public void setAddress(home.object.Address address) {
this.address = address;
}

public java.lang.Integer getEmpNo() {
return empNo;
}

public void setEmpNo(java.lang.Integer empNo) {
this.empNo = empNo;
}

public java.lang.String getName() {
return name;
}

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

private java.lang.Object __equalsCalc = null;
public synchronized boolean equals(java.lang.Object obj) {
if (!(obj instanceof Employee)) return false;
Employee other = (Employee) obj;
if (obj == null) return false;
if (this == obj) return true;
if (__equalsCalc != null) {
return (__equalsCalc == obj);
}
__equalsCalc = obj;
boolean _equals;
_equals = true &&
((this.address==null && other.getAddress()==null) ||
(this.address!=null &&
this.address.equals(other.getAddress()))) &&
((this.empNo==null && other.getEmpNo()==null) ||
(this.empNo!=null &&
this.empNo.equals(other.getEmpNo()))) &&
((this.name==null && other.getName()==null) ||
(this.name!=null &&
this.name.equals(other.getName())));
__equalsCalc = null;
return _equals;
}

private boolean __hashCodeCalc = false;
public synchronized int hashCode() {
if (__hashCodeCalc) {
return 0;
}
__hashCodeCalc = true;
int _hashCode = 1;
if (getAddress() != null) {
_hashCode += getAddress().hashCode();
}
if (getEmpNo() != null) {
_hashCode += getEmpNo().hashCode();
}
if (getName() != null) {
_hashCode += getName().hashCode();
}
__hashCodeCalc = false;
return _hashCode;
}

// Type metadata
private static org.apache.axis.description.TypeDesc typeDesc =
new org.apache.axis.description.TypeDesc(Employee.class);

static {
typeDesc.setXmlType(new javax.xml.namespace.QName("http://object.home/", "employee"));
org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("address");
elemField.setXmlName(new javax.xml.namespace.QName("", "address"));
elemField.setXmlType(new javax.xml.namespace.QName("http://object.home/", "address"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("empNo");
elemField.setXmlName(new javax.xml.namespace.QName("", "empNo"));
elemField.setXmlType(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/encoding/", "int"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
elemField = new org.apache.axis.description.ElementDesc();
elemField.setFieldName("name");
elemField.setXmlName(new javax.xml.namespace.QName("", "name"));
elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
elemField.setMinOccurs(0);
typeDesc.addFieldDesc(elemField);
}

/**
* Return type metadata object
*/
public static org.apache.axis.description.TypeDesc getTypeDesc() {
return typeDesc;
}

/**
* Get Custom Serializer
*/
public static org.apache.axis.encoding.Serializer getSerializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanSerializer(
_javaType, _xmlType, typeDesc);
}

/**
* Get Custom Deserializer
*/
public static org.apache.axis.encoding.Deserializer getDeserializer(
java.lang.String mechType,
java.lang.Class _javaType,
javax.xml.namespace.QName _xmlType) {
return
new org.apache.axis.encoding.ser.BeanDeserializer(
_javaType, _xmlType, typeDesc);
}

}

--------------------------------------------------------------------------------------
/**
* EmployeeWebService.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public interface EmployeeWebService extends java.rmi.Remote {
public void displayEmp(home.object.Employee employee) throws java.rmi.RemoteException;
}

--------------------------------------------------------------------------------------
/**
* EmployeeWebServiceBindingStub.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public class EmployeeWebServiceBindingStub extends org.apache.axis.client.Stub implements home.object.EmployeeWebService {
private java.util.Vector cachedSerClasses = new java.util.Vector();
private java.util.Vector cachedSerQNames = new java.util.Vector();
private java.util.Vector cachedSerFactories = new java.util.Vector();
private java.util.Vector cachedDeserFactories = new java.util.Vector();

static org.apache.axis.description.OperationDesc [] _operations;

static {
_operations = new org.apache.axis.description.OperationDesc[1];
org.apache.axis.description.OperationDesc oper;
oper = new org.apache.axis.description.OperationDesc();
oper.setName("displayEmp");
oper.addParameter(new javax.xml.namespace.QName("http://object.home/", "Employee"), new javax.xml.namespace.QName("http://object.home/", "employee"), home.object.Employee.class, org.apache.axis.description.ParameterDesc.IN, false, false);
oper.setReturnType(org.apache.axis.encoding.XMLType.AXIS_VOID);
oper.setStyle(org.apache.axis.constants.Style.DOCUMENT);
oper.setUse(org.apache.axis.constants.Use.LITERAL);
_operations[0] = oper;

}

public EmployeeWebServiceBindingStub() throws org.apache.axis.AxisFault {
this(null);
}

public EmployeeWebServiceBindingStub(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
this(service);
super.cachedEndpoint = endpointURL;
}

public EmployeeWebServiceBindingStub(javax.xml.rpc.Service service) throws org.apache.axis.AxisFault {
if (service == null) {
super.service = new org.apache.axis.client.Service();
} else {
super.service = service;
}
java.lang.Class cls;
javax.xml.namespace.QName qName;
java.lang.Class beansf = org.apache.axis.encoding.ser.BeanSerializerFactory.class;
java.lang.Class beandf = org.apache.axis.encoding.ser.BeanDeserializerFactory.class;
java.lang.Class enumsf = org.apache.axis.encoding.ser.EnumSerializerFactory.class;
java.lang.Class enumdf = org.apache.axis.encoding.ser.EnumDeserializerFactory.class;
java.lang.Class arraysf = org.apache.axis.encoding.ser.ArraySerializerFactory.class;
java.lang.Class arraydf = org.apache.axis.encoding.ser.ArrayDeserializerFactory.class;
java.lang.Class simplesf = org.apache.axis.encoding.ser.SimpleSerializerFactory.class;
java.lang.Class simpledf = org.apache.axis.encoding.ser.SimpleDeserializerFactory.class;
qName = new javax.xml.namespace.QName("http://object.home/", "address");
cachedSerQNames.add(qName);
cls = home.object.Address.class;
cachedSerClasses.add(cls);
cachedSerFactories.add(beansf);
cachedDeserFactories.add(beandf);

qName = new javax.xml.namespace.QName("http://object.home/", "employee");
cachedSerQNames.add(qName);
cls = home.object.Employee.class;
cachedSerClasses.add(cls);
cachedSerFactories.add(beansf);
cachedDeserFactories.add(beandf);

}

private org.apache.axis.client.Call createCall() throws java.rmi.RemoteException {
try {
org.apache.axis.client.Call _call =
(org.apache.axis.client.Call) super.service.createCall();
if (super.maintainSessionSet) {
_call.setMaintainSession(super.maintainSession);
}
if (super.cachedUsername != null) {
_call.setUsername(super.cachedUsername);
}
if (super.cachedPassword != null) {
_call.setPassword(super.cachedPassword);
}
if (super.cachedEndpoint != null) {
_call.setTargetEndpointAddress(super.cachedEndpoint);
}
if (super.cachedTimeout != null) {
_call.setTimeout(super.cachedTimeout);
}
if (super.cachedPortName != null) {
_call.setPortName(super.cachedPortName);
}
java.util.Enumeration keys = super.cachedProperties.keys();
while (keys.hasMoreElements()) {
java.lang.String key = (java.lang.String) keys.nextElement();
_call.setProperty(key, super.cachedProperties.get(key));
}
// All the type mapping information is registered
// when the first call is made.
// The type mapping information is actually registered in
// the TypeMappingRegistry of the service, which
// is the reason why registration is only needed for the first call.
synchronized (this) {
if (firstCall()) {
// must set encoding style before registering serializers
_call.setEncodingStyle(null);
for (int i = 0; i < cachedSerFactories.size(); ++i) {
java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);
javax.xml.namespace.QName qName =
(javax.xml.namespace.QName) cachedSerQNames.get(i);
java.lang.Class sf = (java.lang.Class)
cachedSerFactories.get(i);
java.lang.Class df = (java.lang.Class)
cachedDeserFactories.get(i);
_call.registerTypeMapping(cls, qName, sf, df, false);
}
}
}
return _call;
}
catch (java.lang.Throwable t) {
throw new org.apache.axis.AxisFault("Failure trying to get the Call object", t);
}
}

public void displayEmp(home.object.Employee employee) throws java.rmi.RemoteException {
if (super.cachedEndpoint == null) {
throw new org.apache.axis.NoEndPointException();
}
org.apache.axis.client.Call _call = createCall();
_call.setOperation(_operations[0]);
_call.setUseSOAPAction(true);
_call.setSOAPActionURI("displayEmp");
_call.setEncodingStyle(null);
_call.setProperty(org.apache.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);
_call.setProperty(org.apache.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
_call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
_call.setOperationName(new javax.xml.namespace.QName("", "displayEmp"));

setRequestHeaders(_call);
setAttachments(_call);
java.lang.Object _resp = _call.invoke(new java.lang.Object[] {employee});

if (_resp instanceof java.rmi.RemoteException) {
throw (java.rmi.RemoteException)_resp;
}
extractAttachments(_call);
}

}

--------------------------------------------------------------------------------------
/**
* EmployeeWebServiceService.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public interface EmployeeWebServiceService extends javax.xml.rpc.Service {
public java.lang.String getEmployeeWebServicePortAddress();

public home.object.EmployeeWebService getEmployeeWebServicePort() throws javax.xml.rpc.ServiceException;

public home.object.EmployeeWebService getEmployeeWebServicePort(java.net.URL portAddress) throws javax.xml.rpc.ServiceException;
}

--------------------------------------------------------------------------------------
/**
* EmployeeWebServiceServiceLocator.java
*
* This file was auto-generated from WSDL
* by the Apache Axis WSDL2Java emitter.
*/

package home.object;

public class EmployeeWebServiceServiceLocator extends org.apache.axis.client.Service implements home.object.EmployeeWebServiceService {

// Use to get a proxy class for EmployeeWebServicePort
private final java.lang.String EmployeeWebServicePort_address = "http://127.0.0.1:8080/empoperations/displayEmp";

public java.lang.String getEmployeeWebServicePortAddress() {
return EmployeeWebServicePort_address;
}

// The WSDD service name defaults to the port name.
private java.lang.String EmployeeWebServicePortWSDDServiceName = "EmployeeWebServicePort";

public java.lang.String getEmployeeWebServicePortWSDDServiceName() {
return EmployeeWebServicePortWSDDServiceName;
}

public void setEmployeeWebServicePortWSDDServiceName(java.lang.String name) {
EmployeeWebServicePortWSDDServiceName = name;
}

public home.object.EmployeeWebService getEmployeeWebServicePort() throws javax.xml.rpc.ServiceException {
java.net.URL endpoint;
try {
endpoint = new java.net.URL(EmployeeWebServicePort_address);
}
catch (java.net.MalformedURLException e) {
throw new javax.xml.rpc.ServiceException(e);
}
return getEmployeeWebServicePort(endpoint);
}

public home.object.EmployeeWebService getEmployeeWebServicePort(java.net.URL portAddress) throws javax.xml.rpc.ServiceException {
try {
home.object.EmployeeWebServiceBindingStub _stub = new home.object.EmployeeWebServiceBindingStub(portAddress, this);
_stub.setPortName(getEmployeeWebServicePortWSDDServiceName());
return _stub;
}
catch (org.apache.axis.AxisFault e) {
return null;
}
}

/**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
try {
if (home.object.EmployeeWebService.class.isAssignableFrom(serviceEndpointInterface)) {
home.object.EmployeeWebServiceBindingStub _stub = new home.object.EmployeeWebServiceBindingStub(new java.net.URL(EmployeeWebServicePort_address), this);
_stub.setPortName(getEmployeeWebServicePortWSDDServiceName());
return _stub;
}
}
catch (java.lang.Throwable t) {
throw new javax.xml.rpc.ServiceException(t);
}
throw new javax.xml.rpc.ServiceException("There is no stub implementation for the interface: " + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
}

/**
* For the given interface, get the stub implementation.
* If this service has no port for the given interface,
* then ServiceException is thrown.
*/
public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws javax.xml.rpc.ServiceException {
if (portName == null) {
return getPort(serviceEndpointInterface);
}
String inputPortName = portName.getLocalPart();
if ("EmployeeWebServicePort".equals(inputPortName)) {
return getEmployeeWebServicePort();
}
else {
java.rmi.Remote _stub = getPort(serviceEndpointInterface);
((org.apache.axis.client.Stub) _stub).setPortName(portName);
return _stub;
}
}

public javax.xml.namespace.QName getServiceName() {
return new javax.xml.namespace.QName("http://object.home/", "EmployeeWebServiceService");
}

private java.util.HashSet ports = null;

public java.util.Iterator getPorts() {
if (ports == null) {
ports = new java.util.HashSet();
ports.add(new javax.xml.namespace.QName("EmployeeWebServicePort"));
}
return ports.iterator();
}

}

--------------------------------------------------------------------------------------
Actual Client Program this is the one which initiates locator and access the server
--------------------------------------------------------------------------------------
package home.object;

import java.net.MalformedURLException;
import java.rmi.RemoteException;

import org.apache.axis.AxisFault;

public class Client {

/**
* @param args
*/
public static void main(String[] args) {
Employee employee = new Employee();
employee.setName("Gangadhara Rao D");
employee.setEmpNo(10662);

Address address = new Address();
address.setStreet("3rd Cross");
address.setCity("Bangalore");
address.setPin(111111);

employee.setAddress(address);
try {
java.net.URL url = new java.net.URL("http://127.0.0.1:8080/empoperations/displayEmp");
EmployeeWebServiceBindingStub employeeWebServiceBindingStub = new EmployeeWebServiceBindingStub(url, new org.apache.axis.client.Service());
employeeWebServiceBindingStub.displayEmp(employee);
} catch(MalformedURLException mfurle) {
mfurle.printStackTrace();
} catch(AxisFault af) {
af.printStackTrace();
} catch(RemoteException re) {
re.printStackTrace();
}
}
}
--------------------------------------------------------------------------------------

No comments:

Post a Comment