Reputation: 726
I'm using SpringMVC to implement a REST API. At some point I need to have a hierarchical URI:
/folder/id/children/id/children/...id/children
With variable depth. Using RestEasy it is possible returning Resource from the top method recursively. Is there any way to implement it in SpringMVC? Otherwise have you any any suggestion for that?
The key point is that the children id are unique only at the same level so: the folder will not have 2 children with the same id but, in the whole tree, will exist several item with the same id.
Any help will be very appreciate.
Upvotes: 1
Views: 790
Reputation: 8414
I don't think there is a nice way to do this with Spring. There are two options I can think of but both are a bit ugly...
Write separate controller methods to capture each "depth" you'll encounter of your URI pattern, e.g. @RequestMapping("/children/{id1}/children/{id2}")
. You can then call a common method to process the captured id
s which could be recursive. I would probably choose this option if the max "depth" was only 3, as the code at least would be easy to read/grok for other developers and the amount of copy&paste would be minimal.
Use a regex in a @RequestMapping("/children/{restOfUrl:+}")
template pattern (see docs) and capture the entire path in a single String variable. You then need your own logic to pull apart the repeating units. This is less flexible in terms of being a blunt instrument (capturing everything starting with /children/
and the :+
in the @RequestMapping
is easy to miss, which makes it a bit harder for someone else trying to work out what URIs go with which controller methods.
Upvotes: 2