次の単純な残りが定義されているとします。
@RequestMapping("/user/data")
@ResponseBody
public String getUserDetails(@RequestParam int id) {
...
}
コードの別の部分で問題のあるパス文字列を読み取る方法はありますか (つまり、まったく別の方法から) ? 何かのようなもの:
String restPath = SomeReflection.getPath("getuserdetails"); // received value: "/user/data"
WDYT? ありがとう!
解決しました! 必要な実装は次のとおりです。
public String getUrlFromRestMethod(Class controllerClass, String methodName) {
try {
Method method = controllerClass.getMethod(methodName);
if (method != null && method.isAnnotationPresent(RequestMapping.class)) {
RequestMapping requestMappingAnnotation = method.getAnnotation(RequestMapping.class);
return requestMappingAnnotation.toString();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();//TODO
}
return null;
}