Java 在Spring boot中通过REST端点读取环境变量值
我正在传递环境变量以运行spring引导应用程序 现在,我想公开一个端点,以获取在运行的应用程序中使用的所有环境变量的值,从而调试一些问题Java 在Spring boot中通过REST端点读取环境变量值,java,spring-boot,rest,environment-variables,Java,Spring Boot,Rest,Environment Variables,我正在传递环境变量以运行spring引导应用程序 现在,我想公开一个端点,以获取在运行的应用程序中使用的所有环境变量的值,从而调试一些问题 有没有办法做到这一点?非常感谢您的帮助。您需要这样一个端点 @GetMapping("env") public Map<String, String> getEnv() { return System.getenv(); } @GetMapping(“env”) 公共地图getEnv(){ returnsystem.
有没有办法做到这一点?非常感谢您的帮助。您需要这样一个端点
@GetMapping("env")
public Map<String, String> getEnv() {
return System.getenv();
}
@GetMapping(“env”)
公共地图getEnv(){
returnsystem.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;
}
@Autowired
环境环境;
@GetMapping(“env”)
公共地图环境({
Map Map=newhashmap();
for(PropertySource PropertySource:env.getPropertySources()){
if(映射属性源的属性源实例){
putAll(((mappPropertySource)propertySource.getSource());
}
}
返回图;
}
Hi@James,我尝试了这个方法,但它没有给我运行这个程序时传递给我的应用程序的环境变量。我还想获得application.yml中定义的所有属性的值。您能提供帮助吗?根据问题的要求,更新为包含属性,而不仅仅是环境变量。