Monday, June 29, 2009

Web Service Creation using tools

What are Web Services?
* Web services are application components
* Web services communicate using open protocols
* Web services are self-contained and self-describing
* Web services can be discovered using UDDI
* Web services can be used by other applications
* XML is the basis for Web services
How Does it Work?
* The basic Web services platform is XML + HTTP.
* XML provides a language which can be used between different platforms and languages and still express complex messages and functions.
* The HTTP protocol is the most used Internet protocol.
* Web services platform elements:SOAP (Simple Object Access Protocol)

* UDDI (Universal Description, Discovery and Integration)

* WSDL (Web Services Description Language)We will explain these topics later in the tutorial.

Writing Web Service

Writing a sample Web Service Application. Create POJO classes, the below are the sample Code

/////////////////////////////////////////////////////////////////////////////////


package com.marlabs.gangadhar;
public class Address {
private String street;
private String city;
private Long pin;
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @return the pin
*/
public Long getPin() {
return pin;
}
/**
* @param pin the pin to set
*/
public void setPin(Long pin) {
this.pin = pin;
}
/**
* @return the street
*/
public String getStreet() {
return street;
}
/**
* @param street the street to set
*/
public void setStreet(String street) {
this.street = street;
}
}

////////////////////////////////////////////////////////////////////////////////


package com.marlabs.gangadhar;
public class Employee {
private String name;
private String empno;
private Address address;
/**
* @return the address
*/
public Address getAddress() {
return address;
}
/**
* @param address the address to set
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* @return the empno
*/
public String getEmpno() {
return empno;
}
/**
* @param empno the empno to set
*/
public void setEmpno(String empno) {
this.empno = empno;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}

/ /////////////////////////////////////////////////////////////////////////////////

Write the web service implementation class, below is the sample code

package com.marlabs.gangadhar;

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 = "Employee", targetNamespace = "http://gangadhar.marlabs.com/")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
@WebContext(contextRoot = "/operations", urlPattern = "/sendEmployeeInfo")
@Stateless()
public class EmpWebImp {
@WebMethod(operationName = "sendEmployeeInfo", action = "sendEmployeeInfo")
public void sendEmployeeInfo(@WebParam(name = "Employee", targetNamespace = "http://gangadhar.marlabs.com/", partName = "employee")
Employee employee) {
System.out.println("Employee Name: " + employee.getName());
System.out.println("Employee No : " + employee.getEmpno());
Address address = employee.getAddress();
System.out.println("Street : " + address.getStreet());
System.out.println("City : " + address.getCity());
System.out.println("PIN : " + address.getPin());
}
}

/////////////////////////////////////////////////////////////////////////////////

Once you deploy the above code successfully in any application server, the server will automatically creates the .wsdl file

to see the wsdl file in Jboss server after successfull deployment, type the below url in browser.

http://192.168.33.79:8080/jbossws/ and then press enter you can able to view the services running the JBoss server. If you click on the link which the service is running it will displays the complete WSDL file. Save that wsdl into any one physical location which is the base to write the Web service client program.

Writing Web Service Client

To generate the client side stub and locator classes we have to have wsdlToJava.jar, and path should be set to JDK.

Assume we have saved wsdl file in C:\ the wsdl name is Employee.wsdl

1. Open Command prompt goto C:\

2. Set the Java Path and also set the class path to wsdlToJava.jar file

3. Execute the following command from command prompt.

C:\>java org.apache.axis.wsdl.WSDL2Java Employee.wsdl

4. It will generate all the classes under the appropriate packages.

5. You need to change the URL in the Locator class.

No comments:

Post a Comment