Search code examples
spring-bootyaml

Use "Variable Value" as Key in Springboot application.yml


Consider a below configuration in application.yml

service:
    id: status-service

provider:
    ${service.id}: "Running"

I need to create a key using a value from so that the configuration can show up like

service:
    id: status-service

provider:
    status-service: "Running"

I have tried following but either I get expression as a string or parsing error:

${service.id}
[service.id]
[[service.id]]
[${service.id}]
%service.id%

I am using SpringBoot(2.7) with Gradle 7.6.1


Solution

  • Long story short, I used following notation to use variable value as key in map. So in order to put "status-service" as key in map using the service.id variable:

    '[$service.id]': "Running"
    

    The final YML map will then result as below:

    service:
        id: status-service
    provider:
        status-service: "Running"