小编典典

Spring Boot启动后如何获取所有端点列表

spring-boot

我有一个用Spring
Boot写的休息服务。我想在启动后获取所有端点。我该如何实现?为此,我想在启动后将所有端点保存到数据库(如果它们尚不存在),并使用它们进行授权。这些条目将被注入角色,并且角色将用于创建令牌。


阅读 556

收藏
2020-05-30

共1个答案

小编典典

您可以在应用程序上下文的开头获取RequestMappingHandlerMapping。

public class EndpointsListener implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent) {
            ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
            applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods().forEach(/*Write your code here */);
        }
    }
}

或者,您也可以使用Spring Boot执行器(即使您不使用Spring
Boot也可以使用actutator),该执行器公开了另一个终结点(映射终结点),该终结点列出了json中的所有终结点。您可以点击此端点并解析json以获取端点列表。

https://docs.spring.io/spring-boot/docs/current/reference/html/production-
ready-endpoints.html#production-ready-
endpoints

2020-05-30