Spring Boot启动后如何获取所有端点列表 [英] How to Get All Endpoints List After Startup, Spring Boot

查看:477
本文介绍了Spring Boot启动后如何获取所有端点列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a rest service written with spring boot. I want to get all endpoints after start up. How can i achieve that? Purpose of this, i want to save all endpoints to a db after start up (if they are not already exist) and use these for authorization. These entries will be inject into roles and roles will be used to create tokens.

推荐答案

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

You can get RequestMappingHandlerMapping at the start of the application context.

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以获取端点列表.

Alternately you can also Spring boot actuator(You can also use actutator even though you are not using Spring boot) which expose another endpoint(mappings endpoint) which lists all endpoints in json. You can hit this endpoint and parse the json to get the list of endpoints.

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

这篇关于Spring Boot启动后如何获取所有端点列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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