[英]Spring MVC url matching with regex
I'm trying to use the regex possibilities of Spring MVC's RequestMapping to distinguish between several REST APIs: 我正在尝试使用Spring MVC的RequestMapping的正则表达式来区分几种REST API:
/somepath/{item:[^&?/]+}/{subitem:[^&?/]+}
/somepath/{item:[^&?/]+}
/somepath
In each case the API returns a totally different JSON object. 在每种情况下,API都会返回完全不同的JSON对象。
Spring MVC has the usually useful habit of conflating multiple slashes or ignoring trailing slashes. Spring MVC 通常具有混合多个斜杠或忽略尾部斜杠的有用习惯。 However, I need to get around that in this case. 但是,在这种情况下,我需要解决这个问题。 So I'm using regular expressions to handle the cases with the path variables are empty ie to stop /somepath//asubitem
being matched to the second API rather than the first. 因此,我使用正则表达式来处理路径变量为空的情况,即停止/somepath//asubitem
与第二个API而不是第一个API匹配。
The problem is that when I use a slash character in the regex, the pattern will not match. 问题是,当我在正则表达式中使用斜杠字符时,模式将不匹配。 Just using [^&?]+
matches but obviously this is not quite what I need. 仅使用[^&?]+
匹配项,但显然这不是我所需要的。
Does anyone have an idea why we have this regex behaviour and how to get around it? 有谁知道为什么我们会有这种正则表达式行为以及如何解决它?
You can try this 你可以试试这个
/\//ig; // Matches /
Refer following url for more detail 请参阅以下网址以获取更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.