Spring Boot 如何获取 Controller 方法名和注解信息?

Posted Java技术栈

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 如何获取 Controller 方法名和注解信息?相关的知识,希望对你有一定的参考价值。

方法一

通过request获得用户的URI,再逐一循环判断是否可以操作。只是这种方法很让人难受。

方法二

通过用户要访问的方法来判断是否有权限:

preHandle方法中handler实际为HandlerMethod,(看网上说的有时候不是HandlerMethod),加个instanceof验证吧

可以得到方法名:h.getMethod().getName()

可以得到RequestMapping注解中的值:h.getMethodAnnotation(RequestMapping.class)

这种方法还是不太方便

推荐一个 Spring Boot 基础教程及实战示例:
https://www.javastack.cn/categories/Spring-Boot/

方法三

自定义注解,自定义注解代码:

@Retention(RUNTIME)
@Target(METHOD)
public @interface MyOperation {
    String value() default "";//默认为空,因为名字是value,实际操作中可以不写"value="
}

Controller代码:

@Controller("testController")
public class TestController {
    @MyOperation("用户修改")//主要看这里
    @RequestMapping("test")
    @ResponseBody
    public String test(String id) {
        return "Hello,2018!"+id;
    }
}

拦截器的代码:

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    System.out.println("进入拦截器");
    if(handler instanceof HandlerMethod) {
        HandlerMethod h = (HandlerMethod)handler;
        System.out.println("用户想执行的操作是:"+h.getMethodAnnotation(MyOperation.class).value());
        //判断后执行操作...
    }
    return HandlerInterceptor.super.preHandle(request, response, handler);
}

在每个方法上面加注解太麻烦啦,可以在类上加注解

@Retention(RUNTIME)
@Target(TYPE)
public @interface MyOperation {
    String value() default "";
}

//拦截器中这样获得
h.getMethod().getDeclaringClass().getAnnotation(MyOperation.class);

我可以获取requestMapping,不用创建自定义注解啊,值得注意的是,不要使用GetMapping等,要使用requestMapping。

原文链接:https://blog.csdn.net/howroad/article/details/80220320

版权声明:本文为CSDN博主「howroad」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

近期热文推荐:

1.1,000+ 道 Java面试题及答案整理(2021最新版)

2.别在再满屏的 if/ else 了,试试策略模式,真香!!

3.卧槽!Java 中的 xx ≠ null 是什么新语法?

4.Spring Boot 2.5 重磅发布,黑暗模式太炸了!

5.《Java开发手册(嵩山版)》最新发布,速速下载!

觉得不错,别忘了随手点赞+转发哦!

如何以编程方式从 spring-boot-actuator 获取指标?

【中文标题】如何以编程方式从 spring-boot-actuator 获取指标?【英文标题】:how to get metrics from spring-boot-actuator programmatically? 【发布时间】:2015-11-29 16:26:28 【问题描述】:

我们有一个生产中的弹簧应用程序。它不是 Spring-boot。我找到了this post,了解如何在非 spring-boot 应用程序中使用 spring-boot-actuator。

但是,我们的要求是从 /metrics 端点聚合数据并对其进行一些分析并报告状态指示器。

例如,我们可能会使用堆参数,例如 "heap.committed":480768,"heap.init":262144,"heap.used":294461,"heap":3728384,"threads.peak":37 表示应用程序的状态 - 致命、警告或健康。

这只是一个例子。我们的要求更复杂。事实上,我们已经有一个status endpoint,我们想在其中添加更多信息(基于来自spring-boot-actuator/metrics/health 端点的数据)。

我正在考虑实现这一目标的一种方法是 在应用程序中对/metrics/health 进行REST 调用,收集数据,聚合它们并返回响应。我不认为这是推荐的方式。

如果有一个 bean 可以直接提取这些参数,我会自动装配它并在需要时动态计算它们。 (其实我会安排定期计算)。

我对从/metrics 返回的所有属性感兴趣。 同时我也对/health的以下内容感兴趣。

"diskSpace":"status":"UP","free":386186194944,"threshold":10485760

我应该自动装配哪些 bean 并免费获得这些属性!

谢谢

编辑

这个post 有@Autowired MetricRepository。但由于某种原因,它只返回自定义计数器属性。它不返回堆、内存信息等 例如: Reporting metric counter.calls.get_greeting=4 Reporting metric counter.calls.get_greeting.1=1 Reporting metric counter.calls.get_greeting.2=1 Reporting metric counter.calls.get_greeting.3=1 Reporting metric counter.calls.get_greeting.4=1 Reporting metric counter.status.200.greeting.number=4 Reporting metric counter.status.404.star-star=1

【问题讨论】:

【参考方案1】:

/metrics 的输出由MetricsEndpoint 生成。它以 bean 的形式提供,您可以拥有 @Autowired。调用invoke 应该会给你你想要的数据。

您可以使用HealthEndpoint/health 执行相同的操作。

【讨论】:

如果我自动连接 MetricsEndpoint 是否需要执行任何其他步骤?调用调用是什么?

以上是关于Spring Boot 如何获取 Controller 方法名和注解信息?的主要内容,如果未能解决你的问题,请参考以下文章

Spring中如何获取request的方法汇总及其线程安全性分析

如何在不从 spring-boot-starter-web 继承的情况下在 Spring Boot 中获取 ObjectMapper 实例?

如何在 Spring-Boot-REST 调用中获取用户信息?

如何获取spring boot application.yml里面的值

如何在 Spring Boot 中获取请求 URL

如何在 Spring Boot 中获取 userDetailsS​​ervice