function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew B 1Andrew B 1 

Duplicate urlMapping on @RestResource

I am creating an Apex class for a REST Webservice.

I created one in a previous class (named 'ContactsManager') that has a createContact method and at the beginning has @RestResource(urlMapping='/Contacts/*')

This class is named 'ContactsCrud'

The Developer console is giving me the 'problem' : Duplicate urlMapping on @RestResource 

I'm not sure why I'm getting this as they are in 2 different classes. Can I change the urlMapping to any name I want but it still uses the Contacts class? What do I need to do here?
jigarshahjigarshah
Andrew,

@RESTResource(urlMapping="your_relative_url") associates the exposed relative REST endpoint with the Apex class that contains the business logic to execute, whenever a request is sent to the respective REST Url. In case, multiple classes with the same HTTP verb (GET, PUT, POST etc.) are mapped to the same endpoint, it also results in ambiguity for Salesforce to choose which Http Method to invoke. Hence, mapping the same REST url endpoint to multiple Apex classes may not be supported.

However, you could leverage either multiple REST classes with different URL patterns or use information within the param string to dispatch the appropriate block of business logic. Additionally, you could use versions to have unique Urls map to multiple classes as below. for e.g.
You could have the first REST Url as @RestResource(urlMapping='/v1/Contacts/*') and the second one as @RestResource(urlMapping='/v2/Contacts/*') which will help you keep the urls disctinct.

Moreover, REST urls are case sensitive. To understand more about REST Resource in Apex refer the following Url - https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_rest_resource.htm

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helpd resolve your issue.
Andrew B 1Andrew B 1
jigarshah: Just to clarify, you are saying I can have multiple files point to Contacts, but I have to change the beginning of it first (v1, v2) etc?
jigarshahjigarshah
Andrew

Yes, adding (v1, v2) will make the urls unique. Pointing the same REST Url to multiple classes with the same HTTP actions (GET, PUT, POST, PATCH etc) is not supported.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helpd resolve your issue.