79246199

Date: 2024-12-03 03:41:54
Score: 0.5
Natty:
Report link

For anyone that lands here looking for an answer, but realizes that the current answers are from 2015, this has been improved with newer versions of Spring because of the PathPattern.

https://spring.io/blog/2020/06/30/url-matching-with-pathpattern-in-spring-mvc

PathPattern is compatible with AntPathMatcher syntax except for the following:

Support for additional syntax to match and capture 0 or more path segments at the end, e.g. "/foo/{*spring}". This is useful as a catch-all pattern in REST APIs with access to the captured path segments through a @PathVariable.

Support for "**" for multi-segment matching is only allowed at the end of a pattern. This helps to eliminate most causes of ambiguity when choosing the closest match for a given request.

In particular, a @GetMapping(path = "/foo/{*spring}") did the trick for me (a @RequestMapping would work too), which injects a @PathVariable String spring to your method. So if I called the API with GET /foo/1/2/3, the String spring would equal /1/2/3 (yes, with the leading slash)

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @PathVariable
  • Low reputation (0.5):
Posted by: Mike DeMille