5

I want to create REST URLs like /contact/{contactId}/test/{testId} and want to pull out the variable conatctId and TestId using a PathParam equivalent from Jersey in Apex RestService code.

How do I do that without manually parsing the URL as a String and parsing for each "/"?

8

I don't think there is an equivalent of the @PathParam annotation in Apex. The Apex REST annotations are described here:

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

That said, since you will know your URL mapping from the @RestResource annotation you could roll your own helper class to put them into a map, then you could access something like:

public class PathParam
{
    public static Map<String,String> parse( String req )
    {
        // assuming URL pattern of '/name/value/name/value' e.g. '/contact/*/test/*'
        List<String> tokens = req.split('/');
        Map<String,String> tokensMap = new Map<String,String>();
        for( integer i = 1 ; i < tokens.size() ; i++ )
        {
            tokensMap.put( tokens.get( i ), tokens.get( ++i ) );
        }
        return tokensMap;
    }    
}

Then, you'd at least have convenient access to your parameters:

Id contactId = PathParam.parse(req).get('contact');
Id testId = PathParam.parse(req).get('test');
2

You can use this rest request method to do this

URL is like :/services/apexrest/AccountId

RestRequest req = RestContext.request;
String accountId=req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);

You can keep changing the increment by 1 or 2 to retrieve all the params .

Or other approach will be as query string parameter say your URL is like :

/services/apexrest/OfflineSync?utctimestamp=2008-10-5+12:20:20&syncgroup=inventory&isdeltaload=0

The code to get value of the parameter will be

RestRequest req = RestContext.request;      
    String utctimestamp=req.params.get('utctimestamp');//The time stamp is extracted from Request URI
    String syncgroup=req.params.get('syncgroup');//The sync group information
    String isdeltaload=req.params.get('isdeltaload');

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.