我需要从某个方面的 joinPoint 获取像 POST/PATCH/GET/etc 这样的 http 方法。
@Before("isRestController()")
public void handlePost(JoinPoint point) {
// do something to get for example "POST" to use below
handle(arg, "POST", someMethod::getBeforeActions);
}
From point.getThis.getClass()
,我得到了该调用被拦截的控制器。然后,如果我从中获取方法和注释。这应该足够好了吧?
So point.getThis().getClass().getMethod(point.getSignature().getName(), ???)
我如何获取类参数类型?
以下代码获取所需的控制器方法注释详细信息
@Before("isRestController()")
public void handlePost(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
// controller method annotations of type @RequestMapping
RequestMapping[] reqMappingAnnotations = method
.getAnnotationsByType(org.springframework.web.bind.annotation.RequestMapping.class);
for (RequestMapping annotation : reqMappingAnnotations) {
System.out.println(annotation.toString());
for (RequestMethod reqMethod : annotation.method()) {
System.out.println(reqMethod.name());
}
}
// for specific handler methods ( @GetMapping , @PostMapping)
Annotation[] annos = method.getDeclaredAnnotations();
for (Annotation anno : annos) {
if (anno.annotationType()
.isAnnotationPresent(org.springframework.web.bind.annotation.RequestMapping.class)) {
reqMappingAnnotations = anno.annotationType()
.getAnnotationsByType(org.springframework.web.bind.annotation.RequestMapping.class);
for (RequestMapping annotation : reqMappingAnnotations) {
System.out.println(annotation.toString());
for (RequestMethod reqMethod : annotation.method()) {
System.out.println(reqMethod.name());
}
}
}
}
}
注意:此代码可以进一步优化。作为示例共享以展示可能性
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)