从joinPoint获取HTTP方法 [英] Get HTTP Method from a joinPoint

查看:212
本文介绍了从joinPoint获取HTTP方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个方面,我需要从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆