4

Is there a way to set the defaultValue to a value from the application.properties file?

@ResponseBody
public void test(@RequestParam(name="testValue", defaultValue = <something from application.properties>))
Akcore
  • 119
  • 1
  • 1
  • 10
  • See this question : https://stackoverflow.com/questions/32587551/how-to-make-requestparam-configurable-through-properties-file – Arnaud Jan 03 '22 at 09:53

1 Answers1

1

Declare your defaultValue as a controller variable like this

@Value("${variable.name.in.app.properties}")
private String myDefaultValue;

Then, in your controller action, assign it like this

@ResponseBody
public void test(@RequestParam(name="testValue", defaultValue = myDefaultValue))
dm_tr
  • 4,265
  • 1
  • 6
  • 30
  • 2
    Can you not just `defaultValue = "${variable.name.in.app.properties}"`? – Bohemian Jan 03 '22 at 10:05
  • Never tried that way. Thank you though @Bohemian – dm_tr Jan 03 '22 at 10:06
  • Thanks for the reply. I tried that but it gives me some error that forces the variable to be final. I can test out the direct method. – Akcore Jan 03 '22 at 21:48
  • 2
    No! `defaultValue = myDefaultValue` doesn't work because defaultValue expects a final. @Bohemian `defaultValue = "${variable.name.in.app.properties}"` is the correct solution. – donnadulcinea Feb 28 '23 at 15:06
  • @donnadulcinea what exactly do you mean by "defaultValue expects a final"? – Bohemian Mar 02 '23 at 02:30
  • The defaultValue parameter must be final (you must assign a constant value that can't be changed). So the solution defaultValue = myDefaultValue is not acceptable if myDefaultValue is defined as above, as an injected string value, Spring will raise a error. Your solution (@Bohemian) is the only correct one. – donnadulcinea Mar 03 '23 at 20:14