Salesforce Rest API Dispatcher - Implementing different endpoints in same rest Api class | Salesforce Funda

Basically salesforce have limitation of having only one type of method like (Get, Post) in one class so we can't use more than get one method in apex class so we have one work around in order to use multiple get method in one apex class

API Dispatcher flow



You could use a API class and map it as:

@RestResource(urlMapping='/v1/ApiDispatcher/*')

Because of the url Mapping /v1/ApiDispatcher/* end point getmethod invoke this is basically a API dispatcher method which will redirect the various endpoint method using this api dispatcher,so in order to use it here we need to configure two get method which have different end point endpoint1 and endpoint2 so the url will become like - v1/ApiDispatcher/endpoint1?Parameter=value and for second one is like v1/ApiDispatcher/endpoint2?Parameter=value

In that case, you might do something like:


  
if(RestContext.request.requestUri.endsWith('/endpoint1')) {
  return getSystemStatus();
} else if(RestContext.request.requestUri.endsWith('/endpoint2')){
  return doApplicationLogic();
  }
}


Example Code :-
======
 

@RestResource(urlMapping='/v1/ApiDispatcher/*')
global with sharing class ApplicationAPI {
    @HttpGet
    global static void getmethod() {
        RestResponse res = RestContext.response;
        String Parameterfromurl = RestContext.request.params.get('ParameterName');
        try {
            if(RestContext.request.requestUri.endsWith('/getSomething')) {
                return getSomething();
               }else if(RestContext.request.requestUri.endsWith('/doSomething')){
                return doSomething();
            }
               
        } 
        catch(BadRequestException e) {
            res.statusCode = e.getStatusCode();
            RestUtil.setResErrorBody(res, e.getErrorCode(), e.getMessage());
        }
    }
    
     getSomething(){
     
      }
      
     doSomething(){
     
      }


now you can hit the URL like this :-  Org-Domain/URLMapping/getSomething?ParameterName=value

so for other case :- Org-Domain/URLMapping/doSomething?ParameterName=value
you can refer this two links :-