3

In REST API, can we create multiple @Http Methods (for e.g. @HttpGet) annotations of the same type in a single class? i have a need where i need to write more than one get method in the same class? is that possible and if it is how?

One of my friend started debating with me that it is possible as long as the method signatures are different. For example,

@RestResource(urlMapping='/RestResearch/v1/Test')
global with sharing class RestResearch {

@HttpGet
global static String getAllCourses(String s) {
        RestRequest req  = RestContext.request;
        RestResponse res  = RestContext.response;
        String id  = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        System.debug('id :'+id);
        return 'Success';
}

@HttpGet
global static String getMyCourses(String s1, String s2, String s3, String s4, String s5, String s6) {
        RestRequest req  = RestContext.request;
        RestResponse res  = RestContext.response;
        String id  = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        return 'Success';
}

}

But, i am getting an error "@HttpGet already used elsewhere in this class" if i tried to save it in Eclipse.

Any help is much appreciated...

  • 1
    The error you're getting is basically your answer... – Benj Dec 24 '12 at 14:03
10

Expanding on Mohith's answer, you may only define a single @HttpGet annotated method on your class, but, according to the docs,

Methods annotated with @HttpGet or @HttpDelete should have no parameters. This is because GET and DELETE requests have no request body, so there's nothing to deserialize.

So you should have a single REST method that examines query parameters and responds accordingly:

@RestResource(urlMapping='/RestResearch/v1/Test')
global with sharing class RestResearch {
    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;

        // Was the method called like http://na1.salesforce.com/services/apexrest/RestResearch/v1/Test?s2=something
        String s2 = req.params.get('s2');
        if (s2 != null) {
            // Process s2 version
        } else {
            // Do something else
        }

        return result;
    }
}

Similarly, for a @HttpPost method, you would deserialize the body and examine it:

@HttpPost
global static String doPost(){
    RestRequest req = RestContext.request;
    RestResponse res = Restcontext.response;

    Blob body = req.requestBody;

    Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(body.toString());

    if (m.get('s2')) {
        // Process s2 version
    } else {
        // Do something else
    }

    return result;
}
  • So does this mean that you can't model calls like: PUT /Entity/:id that take the object in the Request body? – Matt Baker Apr 21 '14 at 13:05
  • 1
    You absolutely can - use the @HttpPut annotation, examine the incoming path in RestContext.request.requestURI to get :id, and get the object from RestContext.request.requestBody. All the pieces are there. – metadaddy Apr 21 '14 at 16:02
5

A single Apex class annotated with @RestResource can't have multiple methods annotated with the same HTTP request method. For example, the same class can't have two methods annotated with @HttpGet.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_methods.htm

Please refer the Docs above

0

Two options:-

1 Nest multiple REST classes within a single wrapper class - can use distinct URLs 2 As above include within the parameters of the input a process identifier that routes the processing to the required option.

Personally if I have dozens of web services to code for many different purposes I would opt for SOAP and use helpful method names.... but I expect in many cases we are talking about elaborating from what looked like a simple requirement in later iterations.

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.