Activity › Forums › Salesforce® Discussions › What is RestResource Annotation in Salesforce?
-
What is RestResource Annotation in Salesforce?
Posted by shariq on August 4, 2017 at 9:15 AMWhat is the use of this annotation and how it works?
Parul replied 7 years, 8 months ago 3 Members · 3 Replies -
3 Replies
-
- 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.
- [adinserter block='9']
-
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.