Invoking Salesforce REST based web service from Java
This post will explain the approach to call the Salesfore REST based webservice through java.
I have exposed a Apex class as REST service from salesforce to fetch the account details.
@RestResource(urlMapping='/AccountDetails/*')
global with sharing class AccountDetails {
@HttpGet
global static Account doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
System.debug('Account Id:'+accountId);
Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
return result;
}
}
The communication is of two steps
- getting the access_token
- invoking the service endpoint by attaching the access_token to the request.
To obtain an access token, we will send an HTTP POST request to the authentication endpoint exposed by Salesforce - https://login.salesforce.com/services/oauth2/token with the details client_id, client_secret, username and password.This values can be get from the configured Connected Apps from salesforce.