1.最常用,也是最直接使用方法,通过@PathVariable注解获取
1 @RequestMapping(value = "/test/{a}") 2 public @ResponseBody Object test(@PathVariable String a) { 3 4 return a; 5 }
2.需要通用@PathVariable注解批量获取, 只有注解的参数才能得到
1 @RequestMapping(value = "/test/{a}") 2 public @ResponseBody Object test(@PathVariable String a, HttpServletRequest httpServletRequest) { 3 4 NativeWebRequest webRequest = new ServletWebRequest(httpServletRequest); 5 Map<String, Object> pathVars = (Map<String, Object>) webRequest.getAttribute(View.PATH_VARIABLES, RequestAttributes.SCOPE_REQUEST); 6 7 return pathVars; 8 }
3.批量获取,不需要任何注解
1 @RequestMapping(value = "/test/{a}") 2 public @ResponseBody Object test(HttpServletRequest httpServletRequest) { 3 NativeWebRequest webRequest = new ServletWebRequest(httpServletRequest); 4 Map<String, String> uriTemplateVars = (Map<String, String>) webRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST); 6 return uriTemplateVars; 7 }