当前位置: 首页 > news >正文

Java反射获取所有Controller和RestController类的方法

Java反射获取所有Controller和RestController类的方法

引入三方反射工具Reflections

<dependency><groupId>org.reflections</groupId><artifactId>reflections</artifactId><version>0.10.2</version>
</dependency>

利用反射获取指定包下的Controller类


public class ControllerClassHolder {public static final Reflections REFLECTION = new Reflections("com.example.controller");public static final Set<Class<?>> CLASSES;static {CLASSES = REFLECTION.getTypesAnnotatedWith(Controller.class);CLASSES.addAll(REFLECTION.getTypesAnnotatedWith(RestController.class);)}
}

获取Controller方法

每个http请求基本都会使用@RequestMapping注解

List<Method> methods = new ArrayList<>();for (Class<?> controllerClass : CLASSES) {Method[] declaredMethods = controllerClass.getDeclaredMethods();Set<Method> methods = Arrays.stream(declaredMethods).filter(m -> m.getAnnotation(RequestMapping.class) != null).collect(Collectors.toSet());
}
http://www.lryc.cn/news/124088.html

相关文章:

  • 设计模式--策略模式
  • VSCode使用SSH无密码连接Ubuntu
  • 通过版本号控制强制刷新浏览器或清空浏览器缓存
  • Redis系列(二):深入解读Redis的两种持久化方式
  • CNN之图像识别
  • nvcc not found
  • pdf怎么转换成jpg图片?这几个转换方法了解一下
  • 六轴机械臂码垛货物堆叠仿真
  • text-decoration 使用
  • linux shell快速入门
  • 【Spring源码】小白速通解析Spring源码,从0到1,持续更新!
  • Unity 鼠标实现对物体的移动、缩放、旋转
  • 67Class 的基本语法
  • 企业数字化转型:无形资产占比测算(2007-2021年)
  • [centos]设置主机名
  • 华为OD真题--新学习选址--带答案
  • Qt自定义对话框
  • Python 程序设计入门(018)—— format() 函数的用法详解
  • 演进式架构
  • OCP China Day 2023:五大社区齐聚,加速开源开放创新与落地
  • 【Linux】进程间通信之管道
  • 记录一个正则表达式
  • 用于全局复根和极点查找算法的自适应网格生成器(Matlab代码实现)
  • 修改Linux中SSH的端口
  • Ansible从入门到精通【六】
  • 国企的大数据岗位方向的分析
  • 【MySQL--->数据类型】
  • Ceph部署
  • 打工日记-Vue3+Ts二次封装el-table
  • funbox3靶场渗透笔记