Spring boot environment Variables

fabiovas
HOBBY

10 days ago

Hello.

I have created few variables but I am not able to use them from the application. I am also printing environment variables to see what we have there, but the ones I created does not show. What should I do so They show up in the environment variables for the application?

Solved$10 Bounty

11 Replies

Railway
BOT

10 days ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


wiqayaai
FREE

10 days ago

After adding variables, redeploy the service manually to refresh


wiqayaai
FREE

10 days ago

what language or framework you’re using?


wiqayaai

After adding variables, redeploy the service manually to refresh

fabiovas
HOBBY

10 days ago

i already did several times with different tries


wiqayaai

what language or framework you’re using?

fabiovas
HOBBY

10 days ago

Java, Spring Boot. I am getting the variable in the properties file. Var=${varname}


clashing
FREETop 1% Contributor

10 days ago

It seems that you might need just the "SERVICE VARIABLES" for your case (which you use the same as we use in our development journey). Hop onto the VARIABLES tab for your service (click the service with which you want to interact) on the Railway dashboard. Then click on New Variable, followed by its name & value. Once added, you can refer to it in your code, like you must have been doing locally.

For an exemplary Java server (assume you have added a variable named test), you can do:

@Value("${test}") private String test;

Or

String test = System.getenv("test");

https://docs.railway.com/guides/variables#service-variables

I hope that helps


fabiovas

Java, Spring Boot. I am getting the variable in the properties file. Var=${varname}

wiqayaai
FREE

10 days ago

Make sure you're referencing the exact environment variable name (DB_PASSWORD), not lowercase (db_password won’t work by default).


wiqayaai
FREE

10 days ago

In your Java code (e.g., main()), you can add:

System.getenv().forEach((key, value) -> System.out.println(key + "=" + value));

See if DB_PASSWORD or your variable appears there.

If not — it’s not injected correctly from Railway yet.


clashing

It seems that you might need just the "SERVICE VARIABLES" for your case (which you use the same as we use in our development journey). Hop onto the VARIABLES tab for your service (click the service with which you want to interact) on the Railway dashboard. Then click on New Variable, followed by its name & value. Once added, you can refer to it in your code, like you must have been doing locally. For an exemplary Java server (assume you have added a variable named test), you can do:@Value("${test}") private String test;OrString test = System.getenv("test");https://docs.railway.com/guides/variables#service-variablesI hope that helps

fabiovas
HOBBY

10 days ago

Thank you for your message. Yes, I have a service variable. In the dashboard of the service, in the Variables it says I have 4 service variables. When using the command suggested by wiqayaai (System.getenv().forEach((key, value) -> System.out.println(key + "=" + value));) I don't see those services variables there.


wiqayaai

In your Java code (e.g., main()), you can add:System.getenv().forEach((key, value) -> System.out.println(key + "=" + value));See if DB_PASSWORD or your variable appears there.If not — it’s not injected correctly from Railway yet.

fabiovas
HOBBY

10 days ago

Thank you for your message. I did what you proposed and the service variables are not there.


fabiovas

Thank you for your message. Yes, I have a service variable. In the dashboard of the service, in the Variables it says I have 4 service variables. When using the command suggested by wiqayaai (System.getenv().forEach((key, value) -> System.out.println(key + "=" + value));) I don't see those services variables there.

fabiovas
HOBBY

10 days ago

I have found the problem and your solution is the one that works. I was missing to commit the changes made in the variables, that are outside of that screen, in the canvas. Thank you and all that helped me.


Status changed to Open chandrika 9 days ago


Status changed to Solved chandrika 9 days ago


Spring boot environment Variables - Railway Help Station