Skip to content
Advertisement

How to set default values in application.properties file?

I have a Springboot project that is deployed in Heroku and connects to MongoDB. The MongoDB URI is set in Heroku as an environment variable, and I access it in my application.properties file like this:

spring.data.mongodb.uri=${MONGO_DB_URI}

This works fine when deploying the app in Heroku, but sometimes I want to run the app locally, and if I do that, the app crashes because the variable MONGO_DB_URI does not exist, therefore the connection to the database failed.

Is it possible to add logic that checks if MONGO_DB_URI is null, and if so, use a hardcoded String value instead?

If not, what is the proper way in doing this?

Thanks in advance, I am new to this so any advice would be of great help!

Advertisement

Answer

@Dilermando Lima commented a good solution if you care about having default values assigned with your properties.

That great if it works but as an alternative, if you don’t want default values for any reason (like you don’t want sensitive values stored in source control), you can add JVM boot properties to your run, like so:

-D<PROPERTIES_TO_BE_ADDED>=<VALUE>

So in your example, you’d do:

-Dspring.data.mongodb.uri=localhost.com
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement