Activity Forums Salesforce® Discussions What is RestResource Annotation in Salesforce?

  • Shaharyar

    Member
    August 4, 2017 at 9:23 am
    •  REST annotation enables you to expose an Apex class or an Apex method as a REST resource. In salesforce, there are six REST annotations, that is :

    1). @RestResource(urlMapping=’/your url’)

    2). @HttpGet

    3). @HttpPost

    4). @HttpPut

    5). @HttpPatch

    6). @HttpDelete

    7). @HttpHead

    • Classes with REST annotation should be defined as global.
    • Methods with REST annotation should be defined as global static.

    I am giving you 1 example of HttpGet...

    @HttpGet is used at method level. This method is called when a HTTP GET  request is sent. To expose your apex class as REST resource you should use @RestResource(urlMapping=’/your url’) annotation at class level as shown below.

    @RestResource(urlMapping='/your_url')
    global class restExample {

    @HttpGet
    global static void exampleMethod() {

    // Write your code here
    }
    }

    Here url mapping is relative to https://instance.salesforce.com/services/apexrest/.

    • If your class is in namespace say ‘abc’, then API  url must be as follows:
    •  https://instance.salesforce.com/services/apexrest/abc/you_url
      URL are case-sensitive.
  • shariq

    Member
    August 4, 2017 at 9:34 am

    Thanks Shaharyar,

    It helps a lot.

  • Parul

    Member
    September 15, 2018 at 3:37 pm

    Hi

    The @RestResource annotation is used at the class level and enables you to expose an Apex class as a REST resource.

    These are some considerations when using this annotation:The URL mapping is relative to https://instance.salesforce.com/services/apexrest/.
    A wildcard character (*) may be used.
    The URL mapping is case-sensitive. A URL mapping for my_url will only match a REST resource containing my_url and not My_Url.
    To use this annotation, your Apex class must be defined as global.

     

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos