从joinPoint获取HTTP方法 [英] Get HTTP Method from a joinPoint
问题描述
在一个方面,我需要从joinPoint获取诸如POST/PATCH/GET/etc之类的http方法.
I need to get the http method like POST/PATCH/GET/etc from a joinPoint in an aspect.
@Before("isRestController()")
public void handlePost(JoinPoint point) {
// do something to get for example "POST" to use below
handle(arg, "POST", someMethod::getBeforeActions);
}
从 point.getThis.getClass()
中,我得到拦截了此调用的控制器.然后,如果我从中获取方法,然后添加注释.那应该足够好吧?
From point.getThis.getClass()
, I get the the controller that this call is intercepted. Then if I get the method from it and then annotations. That should be good enough right?
所以 point.getThis().getClass().getMethod(point.getSignature().getName(),???)
我如何获得Class paramaterTypes?
So point.getThis().getClass().getMethod(point.getSignature().getName(), ???)
How do i get Class paramaterTypes?
推荐答案
以下代码获取所需的控制器方法注释详细信息
Following code gets the required controller method annotation details
@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());
}
}
}
}
}
注意:此代码可以进一步优化.作为示例展示了可能性
Note : This code can be further optimized. Shared as an example to demonstrate the possibilities
这篇关于从joinPoint获取HTTP方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!