Search code examples
javaspringrequest-mapping

Spring request mapping containing square brackets


A project I am working on has a @RequestMapping mapping for a rest method that looks like:

  /v{version:[1-9]}

Can somebody explain what this is doing, does it mean the url can contain 1-9 in that place?


Solution

  • The version is a variable name, as a Path Parameter in the URL. The bracketed portion is a regex expression. In this case a single number from any within the range of 1 to 9.

    So that portion of the URL is valid with something like, /v4 and you have access to the numeric portion in your code as a variable named version.