我需要与enter link description here类似的东西
所以我的路径是:/something/else/and/some/more
我想这样映射:
@RequestMapping(value="/something/**", method=RequestMethod.GET)
public String handleRequest(String theRestOfPath){ /***/ }
或者
@RequestMapping(value="/something/**", method=RequestMethod.GET)
public String handleRequest(String[] theRestOfPathArr){ /***/ }
事情是......我希望将**
匹配的所有内容传递给方法:
1.作为一个字符串(theRestOfPath =" / else /和/ some / more"),
2.或作为一个数组(theRestOfPathArr = [" else",""," some"," more"])。
路径变量的数量可能会有所不同,所以我不能这样做:
@RequestMapping(value="/something/{a}/{b}/{c}", method=RequestMethod.GET)
public String handleRequest(String a, String b, String c){ /***/ }
有办法吗?
谢谢:)
--- --- EDIT
我最终得到的解决方案是:
@RequestMapping(value = "/something/**", method = RequestMethod.GET)
@ResponseBody
public TextStory getSomething(HttpServletRequest request) {
final String URI_PATTERN = "^.*/something(/.+?)(\\.json|\\.xml)?$";
String uri = request.getRequestURI().replaceAll(URI_PATTERN, "$1");
return doSomethingWithStuff(uri);
}
答案 0 :(得分:2)
如果在方法中包含HttpServletRequest
作为参数,则可以访问正在使用的路径。即:
@RequestMapping(value="/something/**", method=RequestMethod.GET)
public String handleRequest(HttpServletRequest request){
String pattern = (String) request.getAttribute(
HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
String path = new AntPathMatcher()
.extractPathWithinPattern(pattern, request.getServletPath());
path = path.replaceAll("%2F", "/");
path = path.replaceAll("%2f", "/");
StringTokenizer st = new StringTokenizer(path, "/");
while (st.hasMoreElements()) {
String token = st.nextToken();
// ...
}
}
答案 1 :(得分:0)
Spring MVC中有一个功能可以为你解析。只需使用@PathVariable注释。