How to get a list of endpoints after startup, Spring Download

I have a holiday service written during spring boot. I want to get all the endpoints after launch. How can i achieve this? The purpose of this, I want to save all endpoints in db after startup (if they do not already exist) and use them for authorization. These entries will be entered into roles, and roles will be used to create tokens.

+14
source share
2 answers

You can get RequestMappingHandlerMapping at the beginning 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 */); } } } 

Alternatively, you can also use the Spring boot drive (you can also use the drive, even if you are not using Spring boot), which provides a different endpoint (a mapping endpoint) that lists all the endpoints in json. You can click this endpoint and parse JSON to get a list of endpoints.

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

+17
source

You need 3 steps to set all the end points:

  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 ships with most disabled endpoints, only 2 are available by default:

 /health /info 

If you want to include all endpoints, just set:

 management.endpoints.web.exposure.include=* 

For more information, refer to:

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

  1. go!

Http: // host / drive / mapping

By the way, in Spring Boot 2 Actuator simplifies the security model by combining it with the application.

For more details, refer to this article:

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

0
source

Source: https://habr.com/ru/post/1267015/


All Articles