Find your content:

Search form

You are here

Parse Pathparam equivalent in restService


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 "/"?

Attribution to: Vijay

Possible Suggestion/Solution #1

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 :


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');

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #2

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

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');

Attribution to: Phil Hawthorn
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content