How to Get All Endpoints List After Startup, Spring Boot

前端 未结 5 653
我寻月下人不归
我寻月下人不归 2020-12-05 06:34

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 af

5条回答
  •  时光说笑
    2020-12-05 06:47

    As an addition to the above comments, since Spring 4.2 you may use the @EventListener annotation like this:

    @Component
    public class EndpointsListener {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(EndpointsListener.class);
    
        @EventListener
        public void handleContextRefresh(ContextRefreshedEvent event) {
            ApplicationContext applicationContext = event.getApplicationContext();
            applicationContext.getBean(RequestMappingHandlerMapping.class)
                .getHandlerMethods().forEach((key, value) -> LOGGER.info("{} {}", key, value));
        }
    }
    

    If you want to find out more about how to use the Spring Events and to create custom events, please check out this article: Spring Events

提交回复
热议问题