How to exposure spring boot actuator endpoints without using application.properties file?

Viewed 199

How can I enable and exposure actuator endpoints in Spring boot 2.5.8 application if I don't have access to application.properties file?

It's easy to do with application.properties file, just configuring

management.endpoints.web.exposure.include=*

But if I don't have access to application.properties, how can I do it in code?

3 Answers

You can use command line to do the same.

mvn spring-boot:run -Dspring-boot.run.jvmArguments='Dmanagement.endpoints.web.exposure.include=*'

You can add the properties as environment variables. Just need to switch casing and . to _

MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=*

Finally, I found 2 solutions.

  1. To use SpringApplication.setDefaultProperties() method before run application:

    public class MyApplication {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(MyApplication.class);
            app.setDefaultProperties(Collections.singletonMap("management.endpoints.web.exposure.include", "*"));
            app.run(args);
        }
    }
    
    
  2. Second approach requires access to properties file.

To open properties file with PropertiesConfiguration (this class can both: read properties from file and write new properties/values to file). Checked if property already exist If not exist - add it.

PropertiesConfiguration config = getConfig(filePathToPropertiesFile);
if (null == config.getString("management.endpoints.web.exposure.include")) {
    config.setProperty("management.endpoints.web.exposure.include", "*");
    config.save();
}

All this actions also need to perform in main method before application run.

Related