【问题标题】:Reading environment variables values through a REST endpoint in Spring-boot通过 Spring-boot 中的 REST 端点读取环境变量值
【发布时间】:2021-08-21 11:13:34
【问题描述】:

我正在传递环境变量来运行 Spring Boot 应用程序。

现在我想公开一个端点以获取所有环境变量的值,以便在我正在运行的应用程序中使用它来调试某些问题。

有没有办法做到这一点?任何帮助将不胜感激?

【问题讨论】:

标签: java spring-boot rest environment-variables


【解决方案1】:

你需要这样的端点

@GetMapping("env")
public Map<String, String> getEnv() {
    return System.getenv();
}

如果您还想要所有应用程序属性,请尝试

@Autowired
AbstractEnvironment env;

@GetMapping("env")
public Map<String, Object> env() {
    Map<String, Object> map = new HashMap();
    for (PropertySource<?> propertySource : env.getPropertySources()) {
        if (propertySource instanceof MapPropertySource) {
            map.putAll(((MapPropertySource) propertySource).getSource());
        }
    }
    return map;
}

【讨论】:

  • 嗨@James,我试过这个,但它没有给我在运行它时传递​​给我的应用程序的环境变量。我还想获取 application.yml 中定义的所有属性的值。你能帮忙吗?
  • 更新为包括属性,而不仅仅是问题所要求的环境变量。
猜你喜欢
  • 2017-12-01
  • 2020-05-05
  • 2018-04-12
  • 2019-08-03
  • 1970-01-01
  • 2020-01-18
  • 2021-07-16
  • 2022-01-12
  • 2022-07-29
相关资源
最近更新 更多