Generally there are 6 annotation used to expose an Apex class as a RESTful Web service.
Scenario : Create a simple rest web service, which has a simple web method which will return a string as a response.
Solution: Below code base is a simple rest web service example in Apex.
Now for simplicity, we are simply used @HttpGet annotation for the below web method.
Note: Apex REST supports two formats for representations of resources: JSON and XML.
- @RestResource(urlMapping='/yourUrl') ==> This should be used above class declaration. Where value of urlMapping denotes your endpoint.
- @HttpDelete ==> This should be used above web method declaration.
- @HttpGet ==> This should be used above web method declaration.
- @HttpPatch ==> This should be used above web method declaration.
- @HttpPost ==> This should be used above web method declaration.
- @HttpPut ==> This should be used above web method declaration.
Scenario : Create a simple rest web service, which has a simple web method which will return a string as a response.
Solution: Below code base is a simple rest web service example in Apex.
Now for simplicity, we are simply used @HttpGet annotation for the below web method.
/** * Rest Web Service Example. **/ @RestResource(urlMapping='/RestWSSampleExmpl/*') global with sharing class RestWSSample { @HttpGet global static String getRestSampleResponse() { return 'Success Response.'; } }
Note: Apex REST supports two formats for representations of resources: JSON and XML.