Take this advice for what it is worth, but I strongly urge you to reconsider the use of REST API instead of Java RMI.
Without having solid metrics to compare, I would highly suspect that performance between Java RMI and REST would be similar.
Java RMI is an older technology which is not as widely adopted.
More Java developers likely have experience with REST and web services versus RMI.
Clients of your server are tied to Java technology (think expansion to mobile clients for instance)
Security concerns
These are just a handful of the issues with it. Beyond that, having to debug poorly written legacy RMI applications was a horrifying ordeal for me that has mentally and emotionally scarred me for life. That may make me a little prejudiced.
Is it appropriate to mix some sort of action call with a resource URI (e.g. /collection/123?action=resendEmail
)? Would it be better to specify the action and pass the resource id to it (e.g. /collection/resendEmail?id=123
)? Is this the wrong way to be going about it? Traditionally (at least with HTTP) the action being performed is the request method (GET, POST, PUT, DELETE), but those don't really allow for custom actions with a resource.
I'd rather model that in a different way, with a collection of resources representing the emails that are to be sent; the sending will be processed by the internals of the service in due course, at which point the corresponding resource will be removed. (Or the user could DELETE the resource early, causing a canceling of the request to do the send.)
Whatever you do, don't put verbs in the resource name! That's the noun (and the query part is the set of adjectives). Nouning verbs weirds REST!
I use the querystring portion of the URL to filter the set of resources returned when querying a collection (e.g. /collection?someField=someval
). Within my API controller I then determine what kind of comparison it is going to do with that field and value. I've found this really doesn't work. I need a way to allow the API user to specify the type of comparison they want to perform.
The best idea I've come up with so far is to allow the API user to specify it as an appendage to the field name (e.g. /collection?someField:gte=someval
- to indicate that it should return resources where someField is greater than or equal to (>=
) whatever someval
is. Is this a good idea? A bad idea? If so, why? Is there a better way to allow the user to specify the type of comparison to perform with the given field and value?
I'd rather specify a general filter clause and have that as an optional query parameter on any request to fetch the contents of the collection. The client can then specify exactly how to restrict the set returned, in whatever way you desire. I'd also worry a bit about the discoverability of the filter/query language; the richer you make it, the harder it is for arbitrary clients to discover. An alternative approach which, at least theoretically, deals with that discoverability issue is to allow making restriction sub-resources of the collection, which clients obtain by POSTing a document describing the restriction to the collection resource. It's still a slight abuse, but at least it's one you can clearly make discoverable!
This sort of discoverability is one of the things that I find least strong with REST.
I often see URI's that look something like /person/123/dogs
to get the persons dogs. I generally have avoided something like that because in the end I figure that by creating a URI like that you are actually just accessing a dogs collection filtered by a specific person ID. It would be equivalent to /dogs?person=123
. Is there ever really a good reason for a REST URI to be more than two levels deep (/collection/resource_id
)?
When the nested collection is truly a sub-feature of the outer collection's member entities, it is reasonable to structure them as a sub-resource. By “sub-feature” I mean something like UML composition relation, where destroying the outer resource naturally means destroying the inner collection.
Other types of collection can be modeled as an HTTP redirect; thus /person/123/dogs
can indeed be responded to by doing a 307 that redirects to /dogs?person=123
. In this case, the collection isn't actually UML composition, but rather UML aggregation. The difference matters; it is significant!
Best Answer
What you call single REST APIs might be called REST API's particular set of resources or resources. You also could look at it as REST API's functionalities. Such as any kind of software, the whole package is versioned/updated, not single functionalities or resources.
Your question would make sense in the context where the REST API package's resources are modular and so potentially developed and versioned separately.
Then, as far as I see, the main cons of your proposed resource locator naming convention are:
When building an API, one of your main objectives is making it easy to use...
You might find a better way to introduce a breaking change or even versioning the REST API with maybe a HTTP header?
To know a little more about HTTP headers approach, see other answers below and: https://www.troyhunt.com/your-api-versioning-is-wrong-which-is/