我必须使用@PathValiable从URL获取params SpringBoot </跨度> 应用。这些参数常常有斜线。我无法控制用户在URL中输入的内容,因此我希望得到什么……
该 SpringBoot </跨度> 代码很简单:
@RequestMapping( “/模块/ {MODULENAME}”)@ResponseBodypublic String moduleStrings(@PathVariable(“moduleName”)String moduleName)throws Exception {
…
}
所以……
此代码获取完整路径:
@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; }
基于P.J.Meisch的回答,我已经找到了针对我案例的简单解决方案。它也允许考虑 的 几个斜线 强> 在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; }
@RequestMapping("/modules/**")