+ Start a Discussion
Ken KoellnerKen Koellner 

More than one @HttpGet and/or @HttpPost per class

I'd like to set up an API for an Enterprise Integration Service consisting of many Get and Post methods.

It would be nice if I could put them all in one class.  Is there anyone to put multiple @HttpGet and @HttpPost in the same class and have the last part of the path be different for each?
 

Otherwise, I'd need to define a class for each method.

The only other alternative would be to have just one get/post and have the input define what operation to perform and have the get post method act as a dispatcher to call the proper inner method.  I'd rather not do it that way.

-Ken
 

Ashish_SFDCAshish_SFDC
Hi Ken, 


See the documentation, 

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


Regards,
Ashish


AWatsonAWatson
@Ken,

Great question and I too have been wondering how to accomplish multiple GET, POST, etc in a single REST Controller. If anyone either 1) finds a way to accomplish this or 2) learns that SF is planning to incorporate this as a routing enhancement or other please let the "rest" of us know!

Thanks,

Watson
Ken KoellnerKen Koellner
What I ended up doing is putting all my logic in a separate class and then use a wrapper class for each method.  But I also consolidated my methods.

There's one "GET" that always returns a list of objects.  If you want one, you use GET ..../myObjectNames/idOfObject/.  If you want a query, you do something like GET .../myObjectNames?city=NYC.

For operations, I have to do one class per POST operation like --

POST .../myObjectsNames/idOfObject/doSomeAction.

You can declare the pathname .../myObjectNames/*?doSomeAction/ and that works fine.

The whole CRUD paradigm where everything is always created, read, updated, deleted doesn't fit with oeprations.  Not everything is simply save a record.
WatsonWatson
Thanks Ken.

Using the ?city=NYC approach will work and it leads me to think it wouldn't be that tough to write a base class that would handle this style of filtering.  I suspect implementing something like the OData spec in abstract base, let's just call it RESTBase, would standardize the coolness after the ? in the querystring.  With that spec implemented in your abstract base, you would pickup queries like $top, $skip, $orderyby, etc across all your REST resources that choose to extend it.

I then started thinking about your comment on operations and thought about adding to the OData implementation mentioned above something like $action.  Part of me wants the convenience of action based REST routing to handle operations like you mentioned, but the other part of me says wait.  Maybe my resource objects, I lean towards simple DTOs, need a better RESTful design?  This way, I'm not stuck thinking about my web service in terms of verbs like SOAP conditioned me to.

Time to take this to the dev sandbox and try some things out!
Ankit Gupta SFDCLearnerAnkit Gupta SFDCLearner
Hi Ken,

Please check the below URL.

https://salesforce.stackexchange.com/questions/64982/apex-rest-class-why-cant-i-have-multiple-apex-methods-for-the-same-http-metho

Thanks,
Salesforce Learner.