2

I am using yaml properties file for the sping boot configuration.

My structure of application.yml is following:

information:
  server: ${SERVER_INFO:devserver}
  config:
    "[http://www.myshop.com]":
      - Sam
      - Joe
    "[https://www.google.com]":
      - Mary
   ... other properties of Map type.

All this values basically represent structure Map<String, List<String>> where key is site address and List is a array of the users. There could be many entries of this map, I am using this structure to read properties dynamicly.

How my docker container looks:

app:
  ports:
    - "8080:8080"
  build:
    ... i will skip this info
  image: testapp
  environment:
    - SERVER_PORT=8080
    - SERVER_INFO=QAAserver // overrided sucessfully
    - INFORMATION_CONFIG=?? // how to pass Map<Sting, List<String>> here?

So, basically, I need an ability to pass values of Map<String, List<String>> from docker compose env var to the spring boot, to override current value. How can I do it?

1 Answers1

1

JSON Objects are Map<String, Object> values.

    INFORMATION_CONFIG='{"[http://www.myshop.com]": ["Sam", "Joe"], "[https://www.google.com]": ["Mary"]}'

But you may want to use SPRING_APPLICATION_JSON for this

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.application-json

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Sounds like a plan, but it does not work for me. When I am using yml and using your solution, it does not work – somerandomguy00 Jul 26 '22 at 06:37
  • 1
    But on the other hand idea of using JSON env variable in this case is acceptable. I will just change my yaml map to single property and after it I will process it. – somerandomguy00 Jul 26 '22 at 07:06
  • Environment variables are not applied to the YAML by default. They'd need to explicitly be loaded using `${INFORMATION_CONFIG}`, as shown in the docs – OneCricketeer Jul 26 '22 at 14:12
  • Yes, but if I am not mistaken spring does provide an ability to override env variables by defaultm without ${} – somerandomguy00 Jul 26 '22 at 17:18
  • No, I don't think so. You can refer the top of the linked externalized configuration documentation. Java System properties using `-D` JVM flags or `SPRING_APPLICATION_JSON` can be used without needing `${}` – OneCricketeer Jul 26 '22 at 20:37