REST WEB SERVICES
REST:
Stands for: Representational state transfer
Rest is :
Ø Stateless
Ø Client – server based
Ø HTTP Based
Ø It is not a protocol
Ø Support Both JSON and XML
@RestResource:
In Rest web
services we need to implement @Rest Resource annotation .
We need to
map endpoint URL here .
Syntax:
@RestResource(urlMapping =’/endpoint url/*’..
Then we need
to implement class with following Http methods
@Httppost = create
@Httpget =
read
@Httpput =
update
@Httpatch =
update
@Httpdelete
= delete
Then we need
implement following
Restcontext
: it is a container class it holds twoobjects
Restrequest: params,requestbody,requestpath
Restresponse:
return messages etc
@RestResource(urlMapping
= '/v29/account/*')
global class restwebservices{
@Httpget
global static account getdetails(){
Restrequest req = restcontext.request;
Restresponse res =
restcontext.response;
string name =
req.requestURI.substring(req.requestURI.lastindexof('/')+1);
account a = [select id,name,phone,type,website,rating
from account where name =: name];
return a;
}
@Httppost
global static id createaccont(string
name,string phone,string website){
Restrequest req = restcontext.request;
Restresponse res = restcontext.response;
account a = new account();
a.name = name;
a.phone = phone;
a.website = website;
insert a;
return a.id;
}
}
No comments:
Post a Comment