EDITED: It works sometimes without any problem (even with the oldest Api 22.0 that supports RestResource) and sometimes incorrectly.
That depends on the order of classes sorted by the time of the last modification.
If you edit the class REST1.cls or generally that with the shortest urlMapping length (add a comment into) it starts to work as you expect.
It is not exactly incorrect, because *
matches any string with any number of /
. It seems that the text "the patterns with wildcards... select the longest (by string length)" should be understand only "the longest string before the wildcard". The order of compared matching is not explicitely defined by docs if these parts are the same.
They mean only: /word/*
is a longer and better match than /*
.
They want to provide a fast service. Therefore they probably need firstly to do a coarse match by an index, then to try classes ordered by this index until the first match is found. If you can find a good index expression than it can be probably fixed, but it is much complicated by rules about the final slash. I'm skeptical of anything, except for asking a possible very small improvement in documentation.
Tested code exemples:
REST1
@RestResource(urlmapping='/invoices/*/')
global class REST1 {
@HttpGet
global static String getClassName() {
return 'REST1';
}
}
REST2
@RestResource(urlmapping='/invoices/*/totals/')
global class REST2 {
@HttpGet
global static String getClassName() {
return 'REST2';
}
}
REST3
@RestResource(urlmapping='/invoices/*/related/')
global class REST3 {
@HttpGet
global static String getClassName() {
return 'REST3';
}
}
It is better and more universal to use urlMapping
string without the final slash, but is is not important for this question. Example:
@RestResource(urlmapping='/invoices/*')
It can catch both /invoices/something/
and /invoices/something
, while the mapping with the final slash requires only this one. Every example in Apex docs (e.g. the qouted above) is without any final slash.
**EDITED ** because the problem was not initially reproducible.
Best Answer
I found the problem as I make my site secure by HTTPS and was doing a request on HTTP, So the post request was discarded.
Thanks