Spring Boot启动后如何获取所有端点列表

新手上路,请多包涵

我有一个用 spring boot 编写的休息服务。我想在启动后获取所有端点。我怎样才能做到这一点?为此,我想在启动后将所有端点保存到数据库中(如果它们尚不存在)并使用它们进行授权。这些条目将注入到角色中,角色将用于创建令牌。

原文由 barbakini 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 506
2 个回答

您可以在应用程序上下文的开头获取 RequestMappingHandlerMapping。

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

或者,您也可以使用 Spring boot actuator(即使您没有使用 Spring boot,您也可以使用 actutator),它公开另一个端点(映射端点),它在 json 中列出所有端点。您可以点击此端点并解析 json 以获取端点列表。

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

原文由 Praneeth Ramesh 发布,翻译遵循 CC BY-SA 4.0 许可协议

您需要 3 个步骤来公开所有端点:

  1. 启用 Spring Boot 执行器
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

  1. 启用端点

在 Spring Boot 2 中,Actuator 禁用了大多数端点,默认情况下仅有的 2 个可用:

 /health
/info

如果要启用所有端点,只需设置:

 management.endpoints.web.exposure.include=*

更多详情,请参考:

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

  1. 去!

http://主机/执行器/映射

顺便说一句,在 Spring Boot 2 中,执行器通过将其与应用程序模型合并来简化其安全模型。

更多详细信息,请参考这篇文章:

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

原文由 karl li 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题