Pages

Tuesday, December 13, 2016

Rest Web Services

REST: Representational State Transfer

REST is:
  • Stateless
  • Client-Server based
  • Http based
  • It is not a protocol
  • Supports both JSON and XML
In Rest Web Service we need to implement @RestResource annotation where we need to map end point URL.
Then we need to implement class with following methods
@Httppost = create
@Httpget = read
@Httpput = update
@Httppatch = update
@Httpdelete = delete
Then we need to implement the following
Restcontext: it is a container class it holds two objects
Restrequest: param,requestbody,requestpath
Restresponse: return message 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;
        }

  }

Open https://workbench.developerforce.com/restExplorer.php and enter the payload as shown below


You can see that an account record has been created.

No comments:

Post a Comment