Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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...

share|improve this question
    
The error you're getting is basically your answer... –  Benj Dec 24 '12 at 14:03
add comment

2 Answers

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;
}
share|improve this answer
    
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 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 at 16:02
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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