java SpringBoot 中的@PathVariable,URL 中带有斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43470421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@PathVariable in SpringBoot with slashes in URL
提问by Kirill Ch
I have to get params from URL using @PathValiable in SpringBoot application. These params often have slashes. I don't have a control about what a user would enter in URL so I would like to get what he has entered and then I can handle with it.
我必须在 SpringBoot 应用程序中使用 @PathValiable 从 URL 获取参数。这些参数通常有斜杠。我无法控制用户在 URL 中输入的内容,所以我想获取他输入的内容,然后我可以处理它。
I have already looked through materials and answers here, I don't think that for me the good solution is to ask users somehow encode the entering params.
我已经在这里浏览了材料和答案,我认为对我来说最好的解决方案不是让用户以某种方式对输入的参数进行编码。
The SpringBoot code is simple:
SpringBoot 代码很简单:
@RequestMapping("/modules/{moduleName}")
@ResponseBody
public String moduleStrings (@PathVariable("moduleName") String moduleName) throws Exception {
...
}
So the URL for example would be the following:
因此,例如 URL 将如下所示:
http://localhost:3000/modules/...
The issue is that the param moduleNameoften has slashes. For example,
问题是参数moduleName经常有斜线。例如,
metadata-api\cb-metadata-services OR
app-customization-service-impl\modules\expand-link-schemes\common\app-customization-service-api
So a user definetely can enter:
因此,用户绝对可以输入:
http://localhost:3000/modules/metadata-api\cb-metadata-services
Is this possible to get everything what a user has entered in URL after /modules/?
这是否可以获取用户在/modules/之后在 URL 中输入的所有内容?
If anyone tell me what are the good ways to handle such issue.
如果有人告诉我处理此类问题的好方法是什么。
回答by Kirill Ch
Basing on P.J.Meisch's answer I have come to the simple solution for my case. Also it allows to take into account several slashesin the URL param. It doesn't allow to work with backslashes as in the previous answer too.
根据 PJMeisch 的回答,我为我的案例找到了简单的解决方案。它还允许考虑URL 参数中的几个斜杠。它也不允许像上一个答案一样使用反斜杠。
@RequestMapping(value = "/modules/**", method = RequestMethod.GET)
@ResponseBody
public String moduleStrings(HttpServletRequest request) {
String requestURL = request.getRequestURL().toString();
String moduleName = requestURL.split("/modules/")[1];
return "module name is: " + moduleName;
}
回答by P.J.Meisch
This code gets the complete path:
此代码获取完整路径:
@RequestMapping(value = "/modules/{moduleBaseName}/**", method = RequestMethod.GET)
@ResponseBody
public String moduleStrings(@PathVariable String moduleBaseName, HttpServletRequest request) {
final String path =
request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
final String bestMatchingPattern =
request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
String arguments = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path);
String moduleName;
if (null != arguments && !arguments.isEmpty()) {
moduleName = moduleBaseName + '/' + arguments;
} else {
moduleName = moduleBaseName;
}
return "module name is: " + moduleName;
}