0

I want to change some values in my application.yml from my controller class during the runtime based on user response. I am using spring boot.

application.yml

resource: 
   KL-MED123-DEV    #Change to something different depending on user response.
client: 
   CLI-AR234        #Change to something different depending on user response.  

I want to change those values from the controller class.

PingController.java

@GetMapping("/ping")
public ResponseEntity<String> ping(){
/*
Logic to change something from here in the yml file.
*/
}

Thanks in advance

EDIT: Actually the values which are changed in the yaml file will be used again in the yaml file.

  • Does this answer your question? [How change property values at runtime in Spring](https://stackoverflow.com/questions/38761781/how-change-property-values-at-runtime-in-spring) – İsmail Y. Jun 11 '22 at 12:06
  • Thanks for your response. Actually i am not planning to use any default value and any placeholder value in any other place except in yaml file itself. – Rahul Maurya Jun 11 '22 at 13:00

1 Answers1

0

In your requirement when you will update the yaml properties based on the user input. So basically you have to reload the Environment object with the updated yaml file. so that updated value can be reflected to other places.

For that you can use Apache Common Configurations library to update the yml properties at run time. You can use PropertiesConfiguration with different ReloadingStrategy.

Below is the link for the reference to implement the same. https://www.baeldung.com/spring-reloading-properties

KapilGahlot
  • 166
  • 3