How to Get All Endpoints List After Startup, Spring Boot

barbakini :

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.

Praneeth Ramesh :

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 */);
        }
    }
}

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=419861&siteId=1