gpt4 book ai didi

java - 从 joinPoint 获取 HTTP 方法

转载 作者:行者123 更新时间:2023-12-02 01:08:13 27 4
gpt4 key购买 nike

我需要从某个方面的 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);
}

point.getThis.getClass(),我得到了该调用被拦截的 Controller 。然后,如果我从中获取方法和注释。这应该足够好了吧?

所以point.getThis().getClass().getMethod(point.getSignature().getName(),???)我如何获取类参数类型?

最佳答案

以下代码获取所需的 Controller 方法注释详细信息

    @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());
}
}
}
}
}

注意:此代码可以进一步优化。作为示例共享以展示可能性

关于java - 从 joinPoint 获取 HTTP 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com