Java Spring Boot启动后如何获取所有端点列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43541080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get All Endpoints List After Startup, Spring Boot
提问by 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.
我有一个用 spring boot 编写的休息服务。我想在启动后获取所有端点。我怎样才能做到这一点?为此,我想在启动后将所有端点保存到数据库(如果它们尚不存在)并使用它们进行授权。这些条目将注入角色,角色将用于创建令牌。
采纳答案by Praneeth Ramesh
You can get RequestMappingHandlerMapping at the start of the application context.
您可以在应用程序上下文的开始处获取 RequestMappingHandlerMapping。
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.
或者,您也可以使用 Spring Boot 执行器(即使您没有使用 Spring Boot,您也可以使用执行器),它公开另一个端点(映射端点),其中列出了 json 中的所有端点。您可以点击此端点并解析 json 以获取端点列表。
回答by karl li
You need 3 steps to exposure all endpoints:
您需要 3 个步骤来公开所有端点:
- enable Spring Boot Actuator
- 启用 Spring Boot 执行器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- enable endpoints
- 启用端点
In Spring Boot 2, Actuator comes with most endpoints disabled, the only 2 available by default are :
在 Spring Boot 2 中,Actuator 禁用了大多数端点,默认情况下只有 2 个可用:
/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
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
- go!
- 走!
btw, In Spring Boot 2, Actuator simplifies its security model by merging it with the application one.
顺便说一句,在 Spring Boot 2 中,Actuator 通过将其与应用程序 1 合并来简化其安全模型。
For more details, refer to this article:
有关更多详细信息,请参阅这篇文章:
回答by Revnic Robert-Nick
As an addition to the above comments, since Spring 4.2you may use the @EventListener
annotation like this:
作为上述注释的补充,从Spring 4.2 开始,您可以使用这样的@EventListener
注释:
@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
如果您想了解有关如何使用 Spring Events 和创建自定义事件的更多信息,请查看这篇文章:Spring Events
回答by Vivek Dhiman
Late to the party but you can directly use
聚会迟到但可以直接使用
@Autowired
private RequestMappingHandlerMapping requestHandlerMapping;
this.requestHandlerMapping.getHandlerMethods()
.forEach((key, value) -> /* whatever */));
回答by uudaddy
In the application.properties, we need management.endpoints.web.exposure.include=mappings
在 application.properties 中,我们需要 management.endpoints.web.exposure.include=mappings
Then we can see all the endpoints at: http://localhost:8080/actuator/mappings
然后我们可以看到所有的端点: http://localhost:8080/actuator/mappings