Spring boot environment Variables
fabiovas
HOBBYOP

4 months 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

4 months 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!


muhammed1447
FREE

4 months ago

After adding variables, redeploy the service manually to refresh


muhammed1447
FREE

4 months ago

what language or framework you’re using?


muhammed1447

After adding variables, redeploy the service manually to refresh

fabiovas
HOBBYOP

4 months ago

i already did several times with different tries


muhammed1447

what language or framework you’re using?

fabiovas
HOBBYOP

4 months ago

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


clashing
FREE

4 months 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}

muhammed1447
FREE

4 months ago

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


muhammed1447
FREE

4 months 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
HOBBYOP

4 months 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.


muhammed1447

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
HOBBYOP

4 months 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
HOBBYOP

4 months 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 4 months ago


Status changed to Solved chandrika 4 months ago


Loading...