【springboot】项目启动时打印全部接口方法
【springboot】项目启动时打印全部接口方法
·
方法:在你springboot项目的基础上,创建下面的类:
package com.llq.wahaha.listener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* @author llq
* @Description ApplicationListener实现类
*/
@Component
public class ControllerPrinter implements ApplicationListener<ApplicationEvent> {
@Autowired
private ApplicationContext applicationContext;
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("-----------------------");
Arrays.stream(applicationContext.getBeanDefinitionNames())
.filter(name -> applicationContext.getType(name).isAnnotationPresent(RestController.class))
.forEach(controllerName -> {
Class<?> controllerClass = applicationContext.getType(controllerName);
System.out.println("Controller: " + controllerClass.getName());
Arrays.stream(controllerClass.getDeclaredMethods())
.filter(method -> method.isAnnotationPresent(RequestMapping.class))
.forEach(method -> {
System.out.printf("\tMethod: %s%n\t\tRequestMapping: %s%n",
method.getName(),
Arrays.stream(method.getAnnotationsByType(RequestMapping.class))
.map(RequestMapping::value)
.flatMap(Arrays::stream)
.collect(Collectors.joining(", ")));
});
});
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)