Monday, 26 November 2012

Simple Web Service Creation Example Using CXF Framework and SOAP in Eclips

Simple Web Service Development Example Using CXF Framework:---

Required Configuration :
1)JDK 1.5 and above
2)Eclipse
3)CXF 2+

Plug CXF  with the eclipse:

1)Open the eclipse IDE
2)Go to Window menu then click on the Preference
3)Then go to Web service link and select CXF
4)A window will  open where you will add the CXF home path

5)Then click ok

Create a simple web service using cxf:
For this example iam follow a bottum up approach

1)Creat a simple dynamic web project

2)Create a Class SimpleWebServiceImpl

package com.nik.test;

public class SimpleWebServiceImpl {
   
    public  String addMethod(int a,int b){
        return "Your sum is ="+(a+b);
    }

}

3)Right click on the class and go to New->Other -> Web Service link and select Web service from the populated links
4)
Select the web service type ->
Browse the service Implementation Class ->
Edit the configuration by --Selecting the server runtime, Web Service runtime
Select Client Type as Java Proxy ->
Check in the options if You want to publish and moniter the web services..
Clic Next


5)Select the method to publish as a web service and click next


6)Click next
 7)Click next
 8)Start server
9)Click next
 10)Click Finish
 11)The Web Serviice Explorer window open
 12)Click on the method link and add the parameter value and click ok
13)This shows that your web service method is successfully published

Wednesday, 3 October 2012

How to access REST web services as a client

What is REST?
Representational State Transfer (REST)is the method used to create and in order to communicate with the web services. It is primarily used to build Web services that are lightweight, maintainable, and scalable.REST web service used HTTP protocol  in order to perform the four operations  create, read, update and delete by calling the methods post,get,put and delete respectively.

HTTP request includes VERB,URI,HTTP VERSION,Request Header and Request Body 
VERB is one of the HTTP methods like GET, PUT, POST, DELETE, OPTIONS, etc
URI is the URI of the resource on which the operation is going to be performed
HTTP Version is the version of HTTP, generally "HTTP v1.1" .
Request Header contains the metadata as a collection of key-value pairs of headers and their values. These settings contain information about the message and its sender like client type, the formats client supports, format type of the message body, cache settings for the response, and a lot more information.
Request Body is the actual message content. In a RESTful service, that's where the representations of resources sit in a message.

How to access rest web services from client site ?

The example for a get request is given below...

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class RESTWebServiceGet {   
   
    public static void main(String[] args) throws Exception {
        String request = "http://twitter.com/statuses/user_timeline.xml?id=xxx";
    
       //Create http client
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(request);
       
        // Send GET request
        int statusCode = client.executeMethod(method);
       
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        InputStream rstream = null;
       
        // Get the response body
        rstream = method.getResponseBodyAsStream();
       
        // Process the response from Yahoo! Web Services
        BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line+"------");
        }
        br.close();
    }

}