• Post Reply Bookmark Topic Watch Topic
  • New Topic

Developing SOAP Web Service.  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
}
}



Set the JBoss Client lib Jars to your class path and compile, Create the Jar with the help of above files and deploy it into JBoss server, the server automatically creates the WSDL (Web Service Description Language) file, with the help of wsdl and wsdl2java.jar we can generate the client side code.

Command for generating client side code with the help of wsdl2java.jar

wsdl2java -p <Package name> <Server Generated wsdl file>


once you run the above command with the proper wsdl file, it will automatically generates the client required classes, Service Locator and Stub classes in the given package. So finally have to write Client program.

Below is the Example Client Program to call Web Service.

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("Test User");
employee.setEmpNo(22222);

Address address = new Address();
address.setStreet("Lavelly Cross");
address.setCity("US");
address.setPin(342756);

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();
}
}
}



 
Rancher
Posts: 42975
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a question in there somewhere? What's the connection to Tomcat (which this forum is all about)?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!