Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get All Endpoints List After Startup, 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.

like image 920
barbakini Avatar asked Apr 21 '17 11:04

barbakini


People also ask

How do I get all API endpoints in spring boot?

Mapping Endpoints In a Spring Boot application, we expose a REST API endpoint by using the @RequestMapping annotation in the controller class. For getting these endpoints, there are three options: an event listener, Spring Boot Actuator, or the Swagger library.

How do you turn on all actuator endpoints in spring boot?

To enable Spring Boot actuator endpoints to your Spring Boot application, we need to add the Spring Boot Starter actuator dependency in our build configuration file. Maven users can add the below dependency in your pom. xml file. Gradle users can add the below dependency in your build.

How do you list all actuator endpoints?

If you hit the actuator mapping endpoint (http://localhost:8080/actuator/mappings) after running the application, it displays all the request mapping endpoints available. You can also find the sampleHandlerMethod which you have written.


1 Answers

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

like image 59
Praneeth Ramesh Avatar answered Oct 12 '22 01:10

Praneeth Ramesh