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