SpringBoot如何使用@PathVariable获取url中的参数信息呢?
下文笔者讲述在SpringBoot开发中获取url中参数信息的方法分享,如下所示
url中匹配参数信息的方法分享
实现思路:
1.RequestMapping中定义url时,将参数信息采用大括号括起、
2.方法的参数中采用@PathVariable注解,加上url的名称
采用以上两种方式,即可匹配出url中的参数信息
例:url中匹配参数信息的方法分享
@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;
}
注意事项:
以上的配置方法可获取满足所有url的参数获取
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


