How to Get All Endpoints List After Startup, Spring Boot

前端 未结 5 642
我寻月下人不归
我寻月下人不归 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

    0 讨论(0)
  • 2020-12-05 07:00

    Late to the party but you can directly use

    @Autowired
    private RequestMappingHandlerMapping requestHandlerMapping;
    
    this.requestHandlerMapping.getHandlerMethods()
                .forEach((key, value) ->  /* whatever */));
    
    0 讨论(0)
  • 2020-12-05 07:07

    You need 3 steps to exposure all endpoints:

    1. enable Spring Boot Actuator
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    1. enable endpoints

    In Spring Boot 2, Actuator comes with most endpoints disabled, the only 2 available by default are :

    /health
    /info
    

    If you want to enable all of the endpoints, just set:

    management.endpoints.web.exposure.include=*
    

    For more details, refer to:

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

    1. go!

    http://host/actuator/mappings

    btw, In Spring Boot 2, Actuator simplifies its security model by merging it with the application one.

    For more details, refer to this article:

    https://www.baeldung.com/spring-boot-actuators

    0 讨论(0)
  • 2020-12-05 07:11

    In the application.properties, we need management.endpoints.web.exposure.include=mappings

    Then we can see all the endpoints at: http://localhost:8080/actuator/mappings

    0 讨论(0)
  • 2020-12-05 07:12

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

    @Component
    public class EndpointsListener implements ApplicationListener<ContextRefreshedEvent> {
    
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            ApplicationContext applicationContext = event.getApplicationContext();
            applicationContext.getBean(RequestMappingHandlerMapping.class).getHandlerMethods()
                 .forEach(/*Write your code here */);
        }
    }
    

    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

    0 讨论(0)
提交回复
热议问题