我的服务代码:
@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 {
}
我的请求将是
是否可以将“src / main / webapp / ../ .....”变为“路径”变量
答案 0 :(得分:1)
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
.....
}
赞赏任何其他方法......