溶液1
隨着HttpServletRequest。
@RequestMapping(value = "/foo", method = RequestMethod.GET)
public String fooMethod(HttpServletRequest request) {
String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
System.out.println("path foo: " + path);
return "bar";
}
溶液2
隨着reflection。
@RequestMapping(value = "/foo2", method = RequestMethod.GET)
public String fooMethod2() {
try {
Method m = YourClassController.class.getMethod("fooMethod2");
String path = m.getAnnotation(RequestMapping.class).value()[0];
System.out.println("foo2 path: " + path);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return "bar";
}
如果你想要得到的類(而不是方法)的路徑,你可以使用:
String path = YourClassController.class.getAnnotation(RequestMapping.class).value();
解決方案3
隨着@PathVariable。
@RequestMapping(value = {"/{foo3}"}, method = RequestMethod.GET)
public @ResponseBody String fooMethod3(@PathVariable("foo3") String path) {
path = "/" + path; // if you need "/"
System.out.println("foo3 path: " + path);
return "bar";
}
當你說'我看過使用路徑變量,但這不是我真正想要的',你確定。 「@RequestMapping(value = {」/ {path}「},method = RequestMethod.GET) public String getMappingValueInMethod(@PathVariable(」path「)String path){ log.debug(」requested「 +路徑); 返回「bar」; }' –
請詳細瞭解您的使用案例。 –
對不起 - 看起來喬治提出的解決方案將起作用。將關閉這個問題。 – bphilipnyc