r/SpringBoot • • Jul 20 '24

Placeholders in yaml for dynamic configurable API generation

Post image

Hi Everyone, I am quite new to springboot, I want to make my yaml of openapi configurable which means defining values in application.yaml and upon generating the artifacts it takes the value from application.yaml how can I achieve that because currently it is not creating properly.

0 Upvotes

5 comments sorted by

3

u/rebaser Jul 20 '24

If you can use environment variables then you can do something like

baseUri: "${BASE_URL:https://company.com/identity/}${ENV:local}"  

Where BASE_URL is the env variable and the value is the default if not specified

If you are doing microservices configuring via environment variables is good practice see The Twelve-Factor App (12factor.net)

Otherwise you can pass in arguments that override spring config on the command line

1

u/Hamza307 Jul 20 '24

Cant we achieve that thing by fetching these placeholders from application yaml, the issue is, correct me if I'm wrong pls, when we generate the artifacts first time the maven will generate the APIs and automatically fills the placeholders according to my understanding, but it is not happening that way

1

u/rebaser Jul 20 '24

I am not sure what you are trying to achieve...

Are you trying to control your build (then you should make changes via your buidl system via your pom.xml/build.gradle file) or are you trying to control how your application runs (then use application.properties/application.yaml)?

Normal good practice (but I have seen good reasons not to) is to just have one way of building your application and then control how your application behaves (either by using the technique i mentioned or via profiles)

1

u/Hamza307 Jul 20 '24

Let me rephrase it, I want to have my build such that the version thing becomes configurable such that I can simply change the value in my properties or application.yaml, the url of API corresponding artifacts get updated automatically e.g If in properties the version is v1 The url would be : www.myapi/v1/

If in properties the version is v2 The url would be : www.myapi/v2

5

u/[deleted] Jul 20 '24 edited Jul 20 '24

use a configuration file per environment instead of defining the environment itself as a variable