简体   繁体   中英

Reading environment variables values through a REST endpoint in Spring-boot

I am passing environment variables to run the spring boot application.

Now I want to expose an endpoint to get the value of all the environment variables getting used in my running application to debug some issue.

Is there any way to do this? Any help would be appreciated?

You need a endpoint like this

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

If you also want all the application properties try

@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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM