You are on page 1of 2

WebServices: @RestResource: This annotation is used to expose an apex

class / business logic class to the Third Party Applications.


WebService are nothing but the Custom Business Logic, which
can be accessible by the External Systems. We can provide the Note: Always WebService class should be defined with
communication between the different disparate systems, "Global" access specifier. We have to specify the resource
which has been developed by using different programming name for the class by using "@RestResource()" annotation on
languages and running on different platforms. It provides the top of the Class as below.
Platform-independent communication between the Syntax:/Ex:
applications. @RestResource(URLMapping='/<ResourceName>/*'/Account
SF Application <----WebServices ------> Java Application sService/*')
By using WebServices, we can expose the Data(Records), Global Class <ClassName/ AccountsManager>
Business Logics and functionalities to the Third Party Systems. {
By using Apex Programming, we can implement 2 Types of // Write the Business Logic..
WebServices }
1. REST Based WebServices (Now a Days REST is in use) Accessing the Resource: We can access / invoke each
2. SOAP Based WebServices (Simple Object Access Protocol) Resource by using a URI over the "http protocol".
(Old) Syntax:
REST Based WebServices / Apex REST: REST --> https://<ServerNumber>.Salesforce.com/services/apexrest/<
Representational State Transfer ResourceName>
Ex:
By using REST, we can access the Resources over the internet
https://ap16.salesforce.com/services/apexrest/CommonServi
with the help of HTTP protocol. Its a Architectural Style, which
ce
has been purely built upon HTTP protocol. It used the HTTP
We can invoke / test the rest WebServices by using the below
protocol internally to provide the communication between the
tools.
applications.
1. Workbench Tool 2. Postman Tool 3. HP QTP Tool 4. Through
In REST WebServices, each Object, Business Logic and Class Apex Programming 5. By using Third Party Application
and Functionalities should be implemented in the form of a 6. By using Integration Tools
"Resource". Each Resource should be identified by using a @HttpGet Method: This annotation is used to get the required
"Resource Name". The Developer can provide the resource resource information from the Provider to Consumer.
name based on the need. Note: This annotation can be applicable only on method level
(i.e. Procedure / Function). All the WebService methods
Ex: Table Name Resource Name
should be defined with "Global" access specifier, and should
Account 001, Contact 003
be defined as "Static".
Every resource can be accessible by using a URI (Uniform
Syntax: @HttpGet
Resource Identifier).
Global static <ReturnType> <MethodName>()
Ex: https://sample.com/<resourceName>
{
Ex: https://ap16.salesforce.com/001 ---> Account Object
// Write the Business Logic..
Records
}
https://ap16.salesforce.com/500 ---> Case Object
Note: We can't create multiple methods "@HttpGet" inside
Recordshttps://ap16.salesforce.com/apex/ShowAllAccountsP
the WebService class with the same annotation.
age ---> Visualforce Page
RestContext Class: This Class is used to establish the secured
HTTP Protocol provides the below Methods:
communication channel between the Provider and the
1. GET (To Retrieve the Records from the Resource / Object)
Consumer. So that Consumer can pass the required input
2. POST (To Insert the Records into the Resource / Object)
parameters to the Provider.
3. PUT (To Update the records into the Resource)
It contains the below inner Classes.
4. PATCH (To Perform both INSERT + UPDATE operations on
1. RestRequest Class: This Class is used to make a request to
the resource)
the WebService to get the required information.
5. DELETE (To Delete the records from the object / resource)
Note: It will store the input parameters in the form of "Key-
Note: REST based WebServices can provide the results in
Value" pairs. Where Key --> Parameter Name. Value -->
the form of "XML / JSON / TEXT".
Parameter Value.
Upon Creating the REST WebServices, we have to use the Ex: Key : Id Value : '001464564564'
below Annotations.
Property: Params: This property contains all the Input
1. @RestResource() 2. @HttpGet() 3. @HttpPost() Parameters supplied by the Consumer to the WebService
4. @HttpPut() 5. @HttpPatch() 6. @HttpDelete()
Class. It contains the information in the form of a Map • SOAP uses its own security measures. SOAP is based on
Collection. standardized Web service security. Thus, better secured
Syntax: Map<String,String> <objectName> = compared to REST.
RestContext.Request.Params; • SOAP is more reliable in terms of security than REST
RESTful service can use SOAP web services as implementation
2. RestResponse Class: By using this class, we can collect the
• SOAP cannot use RESTful services because SOAP is a
response from the WebService class to the Third Party
protocol.
Application.
• REST uses JSON. JSON is easier to parse than XML. Thus, REST
@HttpDelete Method: By using this method, we can remove
uses less memory and CPU SOAP uses only XML.
either one or more records from the objects with the help of
• XML is harder to parse. SOAP uses more memory and CPU
the WebService.
• REST is best for social media-based Applications.
Syntax: @HttpDelete
• SOAP is best for Enterprise web applications, banking
Global static <ReturnType> <MethodName>()
transactions.
{
// Write the Business Logic to Remove the Records. Oath: (Open Authentication):
}
OAuth is an open protocol that authorizes a client application
@HttpPost Method: By using this method, we can insert
either One / More number of records into the Salesforce to access data from a protected resource through the exchange
object with the help of "WebServices". Upon inserting the of tokens.
records, we have to pass the field values in the form of "{}
Format" by using "Request Body".
Syntax: @HttpPost
Global Static <ReturnType> <MethodName>(<Parameters>)

{
// Write the Business Logic to insert the records.
}
@HttpPut Method: By using this method, we can prepare a
WebService, which allows us to perform the Update
operations on the object records. By using this feature, we can
update either One / More records inside the object. Upon
updating the records, we have to supply the record Id, and the
New Values for the fields to be get updated.
Syntax: @HttpPut
Global Static <ReturnType> <MethodName>(<Parameters>)
{
// Write the Business Logic to Update the Records..
}

@HttpPatch Method: To Perform both INSERT + UPDATE


operations on the resource
Syntax: @HttpPatch
Global Static <ReturnType> <MethodName>(<Parameters>)
{
// Write the Business Logic..
}
Difference Between Rest & Soap API:

• REST is not a protocol; it is an architectural style. SOAP is a


protocol
• REST uses URL to expose the web service & SOAP uses WSDL
class to expose the web service.
• REST allows different data formats: XML, JSON, plain text.
SOAP Allows Only XML format
• REST requires less bandwidth than SOAP. SOAP requires
more bandwidth than REST

You might also like