Select your Language

Apex Rest API

 


    1. If you want to salesforce application to be a service provider then we have the Rest resource in the apex.

    2. How to define apex class as rest service 

    a. We have to annotate the class with  @RestResource annotation

    b. We have to provide the path or URL of the resource.

    @ResetResource(urlmapping='/myServie/*')

    c. Apex class has to be global.

   

    Example : 

    @RestResource(urlmapping='/myServices/*')

    global class Example{ }


Note: If anyone wants to invoke this resource they have define as :   https://ap4.salesforce.com/services/apexrest/myServices

   

    Example : 

    @RestResource(urlmapping='/createAccount/*')

    global class Demo{ }

    

Note: https://ap4.salesforce.com/services/apexrest/createAccount?

    

    d. While defining the method in the rest API service we have to specify what type of service

      @HttpGet

      @HttpPost

      @HttpDelete

      @HttpPut

      @HttpPatch

   

    e. How to define methods in the class .

    1. Every method should be static .

    2. Every method should be global.

    3. Every Method have annotation

    4. we can define only method of each type in a class.

    

    @HttpGet

    global static String call(){

    

    }

    

    @HttpPost

    global static string invoke(String name,String city){

    

    }

   

    f. RestContext: 

    1. This will reffer to the context of restservice call ,this will have request object and response object 

    2. RestContext.request

    3. RestContext.response

    

    g. RestRequest:

    1. This is a pre-defined apex class.

    2. This is defined under the namespace of system.

    3. This class is used set data or retrive the data from the request object.

    Example :

    RestRequest request=new RestResource();

    RestRequest request=Restcontext.request;

    4. Properties :

    a. header: This will contain the all the elements that are passed as header in the request.

:<!--  Map<String,String>-->

    

    b. httpMethod: This will specity type of http request is made from request object.

Example : GET ,POST,PUT,DELETE...

    

    c. params: This will reffer to all the parameters passed in the url of the endpoint

<!-- Map<String,String> -->

    

    d. requestBody: This will return the body of the request body.

Blob requestBody()

    

    e. requestUri: This will return the endpoint of the request .

    Example : https://ap4.salesforce.com/apex/ExampleService

    

    f. resourcePath: This will return the resource path of the request.

    Example  :ExampleService

    


No comments:

Post a Comment