[SalesForce] Calling a method with in a REST class

Excuse me if this may be a bit of a generic question, but the docs aren't clear on this. If I have a REST class called Foo, with a method within called bar(), what is the syntax for calling that method?
https://instance.salesforce.com/apexrest/v27/Foo/bar?

Best Answer

The correct URL would be...

https: //instance.salesforce.com/services/apexrest/Foo/bar

Note: The API version only applies to Salesforce REST URL's

For a HTTP GET request to the above URL you would write your Apex as follows.

@RestResource(urlMapping='/Foo/bar')
global with sharing class Foo 
{ 
    @HttpGet
    global static String bar() 
    {
        return 'bar';
    }   
}

The @RestResource annotation lets you define the URL associated with the class. And then the @HttpGet, @HttpPost etc.. annotations go on your methods. So you don't actually really specify the method name in the URL, its purely a mapping to your class, the method which gets called depends on the HTTP Method specified in the call. So you could change the Apex method to 'bob' and it wouldn't make any difference! :)

@RestResource(urlMapping='/Foo/bar')
global with sharing class Foo 
{
    @HttpGet
    global static String bob() 
    {
        return 'bar';
    }   
}

Finally have a good read through this section of the Apex documentation, defining custom Apex REST services is very well implemented by Salesforce! If you want to learn more about designing great REST API's a can recommend this blog and videos from ApiGee.