Mikhail
Mikhail

Reputation: 769

The correct way to access sub resources in REST service

Let's imagine we have a resource As that contains Bs which in it's turn contains Cs.

To get all the Cs I'd usually create controller method with URL like As/Bs/Cs. And to get a particular B I would do As/Bs/{bId}. But is this correct ?

How someone else would understand that this "Cs" part in the first URL is the name of the sub resource, not the {bId} ? Specially if B has a string id.

Shouldn't it be something like a wildcard symbol, that would make the first query look like As/*/Bs/*/Cs, So you would immediately see what is id and what is the sub resource ?

Upvotes: 0

Views: 1238

Answers (1)

cassiomolin
cassiomolin

Reputation: 131237

Short answer

When a URL matches multiple patterns, a sort is used to find the most specific match. How is it determined? A pattern with a lower count of URI variables and wild cards is considered more specific.

So /servers/deployments/executions is more specific than /servers/deployments/{deploymentId}.

A bit longer answer

The Spring MVC documentation tells you the whole story:

Path Pattern Comparison

When a URL matches multiple patterns, a sort is used to find the most specific match.

A pattern with a lower count of URI variables and wild cards is considered more specific. For example /hotels/{hotel}/* has 1 URI variable and 1 wild card and is considered more specific than /hotels/{hotel}/** which as 1 URI variable and 2 wild cards.

If two patterns have the same count, the one that is longer is considered more specific. For example /foo/bar* is longer and considered more specific than /foo/*.

When two patterns have the same count and length, the pattern with fewer wild cards is considered more specific. For example /hotels/{hotel} is more specific than /hotels/*.

There are also some additional special rules:

  • The default mapping pattern /** is less specific than any other pattern. For example /api/{a}/{b}/{c} is more specific.
  • A prefix pattern such as /public/** is less specific than any other pattern that doesn’t contain double wildcards. For example /public/path3/{a}/{b}/{c} is more specific.

Upvotes: 2

Related Questions