Spring boot with internal React - passing ENV's the right way

Viewed 729

I'm facing the issue with passing system ENV's to the JS app, which is an internal part of Spring Boot App: subproject, which is processed during build, and then passed to the meta of the Java application. Everything is working fine, except that ENV variables totaly don't affects the JS applicaiton. On Java side everything is allright, but whatever I would use to manipulate the system environmentals it just don't work. I tried:

  • export it as a USER's env
  • adding it as a parameter to the application: java -DENV_NAME=abcd app.jar
  • creating the .env file

Did you faced the same problem and found a working solution for that? One more important note here: for JS build process I'm using parcel

My desired state is that when I change the ENV, the JS which lives&runs inside of the .jar could react for this change (e.g. credentials)

1 Answers

Ok, after some brainstorming in our company we choosed some option that looks quite well.

So, to pass choosen system variables the option is to expose them in the controller & action with .json resource containing required variables. Then, those can be inlcuded inside of the front-end JS app just by reading the //somename.json resource somewhere in the very beginning of the script.

Now it's time for the why's:

  • Java app & JS app starts from same, single .jar file so the default host visible from the JS is the same as for Java.
  • .json just contains data, and it is easy to explicit choose only those vars which should be exposed
  • solution is easy to implement

I hope this will be helpfull for someone

Related