Tuesday 17 July 2012

SOAP based Webservices example





Hi Friends........... !!




Previously we saw Overview of SOAP based Webservices. Now lets move to the Example of SOAP based webservices.


In the development of a webservice, we have two parts.
One is developing a service and another is using the developed service (i.e., creating a client for that service).

I. SERVER SIDE:

Lets Create a service named 'SayHello.java'
  • For making 'SayHello' class as a web service annotate the class with '@WebService'
  • Create a 'hello()' method in that class. Annotate it with '@WebMethod' (attributes in it are optional)
  • If needed, annotate the method params with '@WebParams'. Its is also optional.
  • If you want to return the User-defined Objects as the response, you can create and return them as same as in normal procedure.
  • For these User-defined Objects, the Complex Types are automatically generated in the 'xsd' file.
  • This 'xsd' is used in WSDL file, which describes about the service.
The Sample code for the Service is :


com.navin.SayHello.java


package com.navin;

import javax.jws.WebMethod;

import javax.jws.WebParam;
import javax.jws.WebService;
/**
 * @author navin.k
 */
@WebService()
public class SayHello {
    /**
     * Web service operation
     */
    @WebMethod(operationName = "hello")
    public HelloResponse hello(@WebParam(name = "User")
    String user) {
        HelloResponse response = new HelloResponse();
        response.setMessage("Hello " + user);
        return response;
    }
}

com.navin.HelloResponse.java  (user-defined response type for service)

package com.navin;
/**
 * @author navin.k
 */
public class HelloResponse {
    String message;
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}


After Deploying this service, test the application by running the below url:
     url:        http://hostname:port/context-name/webservice-name?wsdl

If WSDL is generated then the service is deployed successful and running fine.



II.  CLIENT SIDE:


Lets check the above created service with a Client.

  • Create a normal Java class
  • Create a Service for the Web Service. (generally appends 'Service' for the web service name)
  • Get the port on the service
  • Call the method on the port by passing all the required parameters for the service 
  • Receive and process the response from the service
The Sample code for the client is:

package com.navin.client;
/**
 * @author navin.k
 */
public class TestClient {
    public static void main(String[] args) {
        HelloResponse response = SayHelloService().getSayHelloPort().hello("JAVA");
        String message = response.getMessage();
        System.out.println("Service response : " + message);
    }
}

The output for the above client is :

Service response : Hello JAVA


Now you are successfully able to run the Web Service........ 

 For more clear steps on creation of Webservice please look into this blog....


To know more about procedure for Creating a Web Service at Server Side check here


To know more about procedure for Creating a Web Service at Client Side check here



No comments:

Post a Comment