我的服务代码:
@RequestMapping(value = "/projects/{projectId}/resources/web/{path}", method = RequestMethod.GET)
@ResponseBody
public void getWebFileContent(@PathVariable("projectId") String projectId,@PathVariable("path") String path, HttpServletRequest httpServletRequest) throws Exception {
}
我的要求是
/ projects / pro1 / resources / web / src / main / webapp
/ projects / pro1 / resources / web / src / main / test / com / pro1 ...
并且可以将“ src / main / webapp / .. / .....”放入“ path”变量中
最佳答案
Spring在URL处理程序映射中提供了三种模式
? -零个或一个字符
*-一个字符
**-一个或多个字符
下面的方法解决了我的问题
@RequestMapping(value = "/projects/{projectId}/resources/web/**", method = RequestMethod.GET)
@ResponseBody
public void getWebFileContent(@PathVariable("projectId") String projectIdHttpServletRequest httpServletRequest) throws Exception {
String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
// will get path = /projects/pro1/resources/web/src/main/webapp
String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
// will get bestMatchPattern = /projects/pro1/resources/web/**
AntPathMatcher apm = new AntPathMatcher();
String exactPath = apm.extractPathWithinPattern(bestMatchPattern, path);
// will get exactPath = src/main/webapp
.....
}
任何其他方法都表示赞赏。