Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

In Dropwizard, how do you put a database password from an environment variable into config.yml, and override the HTTP port at start without editing the file?

In initialize, we wrap the configuration source provider with a SubstitutingSourceProvider that uses an EnvironmentVariableSubstitutor, and then write password: ${DB_PASSWORD} in config.yml, or ${DB_PASSWORD:-value} for a default. Strict mode, new EnvironmentVariableSubstitutor(true), makes startup fail when a variable is missing, which is safer for secrets. To change the HTTP port without editing the file, we pass a system property with the dw. prefix, such as -Ddw.server.applicationConnectors[0].port=9090. Real secrets never go into the committed YAML.

Why this question is asked

Dropwizard services read their settings from a single YAML file, which is convenient until we need different values per environment and secrets that must not live in Git. Container platforms and secret managers deliver those values as environment variables, so we need a way to bring them into the file at startup. Interviewers ask this because it combines configuration hygiene, security and knowledge of how Dropwizard reads its configuration. In this answer, we will see how Dropwizard loads configuration, add environment variable substitution, look at defaults and strict mode, override values with dw. system properties, and close with how to keep secrets out of the repository.

1How Dropwizard Reads Its Configuration

When the server command starts, Dropwizard asks a ConfigurationSourceProvider to open the configuration file, by default a FileConfigurationSourceProvider that reads it from disk. It hands the stream to Jackson's YAML parser, applies any overrides from system properties, maps the result onto our Configuration class and validates it.

The source provider is the extension point. It is just something that turns a path into an input stream, so we can wrap it with another provider that changes the text before Jackson sees it. That is exactly how environment variable substitution works: the YAML is treated as a template whose placeholders are filled in before parsing.

2Adding Environment Variable Substitution

Substitution is not on by default; we enable it in initialize, because that runs before the configuration is read.

Java
@Override
public void initialize(Bootstrap<OrdersConfiguration> bootstrap) {
bootstrap.setConfigurationSourceProvider(
new SubstitutingSourceProvider(
bootstrap.getConfigurationSourceProvider(), // keep the original file reading
new EnvironmentVariableSubstitutor(true))); // true = strict: missing variable fails startup
}

Notice that we wrap the existing provider rather than replacing it. The original still reads the file; the SubstitutingSourceProvider reads its text, replaces each ${NAME} with the value of the environment variable NAME, and passes the result on. The substitutor is built on Commons Text's StringSubstitutor, restricted to environment variables, so it does not have the script and DNS lookups behind the 2022 Commons Text vulnerability.

3Writing the Placeholders in config.yml

Now the YAML file can refer to environment variables. A placeholder can also carry a default value after :-, which is used when the variable is not set.

Java
database:
driverClass: org.postgresql.Driver
url: jdbc:postgresql://${DB_HOST:-localhost}:5432/orders
user: ${DB_USER:-orders}
password: ${DB_PASSWORD} # no default: must come from the environment
$ DB_HOST=db.internal DB_PASSWORD=s3cret java -jar orders.jar server config.yml
# url -> jdbc:postgresql://db.internal:5432/orders
# user -> orders (default used)
# password -> s3cret
$ java -jar orders.jar server config.yml # DB_PASSWORD not set, strict mode
# io.dropwizard.configuration.UndefinedEnvironmentVariableException:
# The environment variable 'DB_PASSWORD' is not defined; could not substitute the expression '${DB_PASSWORD}'.
Step throughFrom config.yml to a validated configuration
  1. FileConfigurationSourceProviderreads config.yml
  2. SubstitutingSourceProviderreplaces ${NAME} and ${NAME:-default}
  3. Variable missing, strict mode?
    YesFail startup, name the variableNoContinue
  4. Jackson parses YAMLdw.* system properties applied
  5. Hibernate Validatorthen run() is called

5 steps in order. Play them, or step through them one at a time.

Substitution happens on text before parsing; overrides apply during mapping.

This is why strict mode matters. With new EnvironmentVariableSubstitutor(false), a missing variable leaves the text ${DB_PASSWORD} in place, so the service starts and later fails to connect with a confusing authentication error, or, worse, a field without validation silently takes the literal placeholder. With strict mode, the service refuses to start and names the missing variable, which is what we want for secrets. Defaults remain available for values that really have a sensible fallback, such as a local host name in development; the exact exception text may differ by version.

4Overriding Values With dw. System Properties

Sometimes we want to change one value for one run without touching the file or setting a variable, for example running a second instance on another port. Dropwizard reads Java system properties whose names start with dw. and treats the rest of the name as a path into the configuration.

Shell
$ java -Ddw.server.applicationConnectors[0].port=9090 \
-Ddw.server.adminConnectors[0].port=9091 \
-jar orders.jar server config.yml
# Started application@...{0.0.0.0:9090}
# Started admin@...{0.0.0.0:9091}
$ java -Ddw.logging.level=DEBUG -jar orders.jar server config.yml # any field can be overridden

The path follows the YAML structure: server, then the applicationConnectors list, index 0, then port. Indexes in square brackets address list elements. The -D options must come before -jar, because anything after the jar name is passed to the application as arguments instead of to the JVM. This works with the default server factory; with the simple server, the path is server.connector.port instead. Overrides are applied after substitution and before validation, so an invalid override is reported like any other configuration error.

5Keeping Secrets Out of the File

The point of all this is that config.yml can be committed safely. It holds structure and non-secret defaults, while secrets arrive at runtime from the platform: Kubernetes secrets mapped into environment variables, a secret manager's agent, or the CI system during tests. A real password in the YAML file would end up in Git history, in build artefacts and in anyone's laptop clone, and removing it later requires rotating the secret, because history is hard to clean.

Two extra precautions help. Environment variables can leak through process listings, crash reports or debug endpoints, so for very sensitive values some teams mount secrets as files and write a small custom substitutor or provider that reads them. And the password field in the configuration class should never be logged; Dropwizard does not print configuration values at startup, but our own code should avoid toString methods that include them.

6Follow-up questions

Can we substitute variables inside a default value?

Yes, if the substitutor is created with substitution in variables enabled, using the two-argument constructor EnvironmentVariableSubstitutor(strict, substitutionInVariables). Then ${DB_URL:-jdbc:postgresql://${DB_HOST}/orders} works. Check your version's constructor, and keep such nesting rare for readability.

What is the difference between environment variable substitution and dw. overrides?

Substitution happens on the file's text before parsing, and only where the file has placeholders. dw. overrides set a configuration path directly during mapping, for any field, without the file mentioning it. Substitution suits values that differ per environment; overrides suit one-off changes on the command line.

Why not read System.getenv directly in the code?

Because then the configuration is split between the file and scattered code, validation annotations do not apply, and the check command cannot verify it. Keeping everything in the Configuration class means one place to read, one validation step and a clear error at startup.

7Conclusion

In this answer, we saw that Dropwizard reads configuration through a source provider that we can wrap. A SubstitutingSourceProvider with an EnvironmentVariableSubstitutor fills ${DB_PASSWORD} placeholders from the environment, with ${NAME:-value} for defaults and strict mode to fail startup when a variable is missing. System properties prefixed with dw., such as -Ddw.server.applicationConnectors[0].port=9090, override any field without editing the file, and real secrets stay out of the committed YAML.

What to say in the interviewAbout 70 seconds aloud

Open with

In initialize, which runs before the configuration is read, I wrap the existing source provider: bootstrap.setConfigurationSourceProvider with a new SubstitutingSourceProvider around bootstrap.getConfigurationSourceProvider and a new EnvironmentVariableSubstitutor. Then the YAML file becomes a template, so I write password: ${DB_PASSWORD}, and for values with a sensible fallback I can write ${DB_HOST:-localhost}.

Then, if they want more

I pass true to the substitutor to make it strict, so if DB_PASSWORD is missing the service refuses to start and names the variable, instead of starting with the literal placeholder and failing later with a confusing database error. That is the safer choice for secrets.

For the port, Dropwizard reads system properties that start with dw. and treats the rest as a path into the configuration, so -Ddw.server.applicationConnectors[0].port=9090 before -jar changes the first application connector's port without editing the file. Overrides are applied before validation, so mistakes are still reported at startup.

The point of all of this is that config.yml can be committed with structure and harmless defaults, while real secrets come from the platform, such as Kubernetes secrets or a secret manager, and never appear in Git.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "In Dropwizard, how do you put a database password from an environment variable into...": https://scrivio-interview-library.netlify.app/java/dropwizard/581f01f58a/