2

I'm trying to implement a web service in apex.

I have two GET methods

  1. GET /application
  2. GET /application/status

1 had been already implemented in the following way. I am given the task to implement 2. Should I insert the logic in the same method below or is there a way to define a new method based on the endpoint?

@RestResource(urlMapping='/v1/application/*')
global with sharing class ApplicationAPI {
    @HttpGet
    global static void get() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        try {
            String appID = RestUtil.getPathParam(req, 'application/');
            if(appID == null) {
                //get multiple apps
            } else {
                //get one app
            }
        } 
        catch(BadRequestException e) {
            res.statusCode = e.getStatusCode();
            RestUtil.setResErrorBody(res, e.getErrorCode(), e.getMessage());
        }
    }
4

You could use a new class and map it as:

@RestResource(urlMapping='/application/status')

Because of the matching rules, /application/status will map to this new class, while /application/anythingElse will map to /application/*. Note that you could choose to parse the RestContext.request.requestUri and make a call to one or more methods based on that, but that's probably overkill in your case.

In that case, you might do something like:

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

(Whatever that means for the context of your application).

1
  • Looks to me like the intent is to parse the URI; IMHO that would be the cleaner way to go here too. – Keith C Apr 15 '20 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.