Issue
I am making an app that requires an argument from the command line using Spring Boot, the passed argument then will be passed on to a field as follow:
@Value("#{new java.text.SimpleDateFormat('dd-MM-yyyy').parse('${param.date}')}")
private Date date;
However, when I tried to create the Jar file using the command mvnw package
, the following error is shown:
java.lang.IllegalArgumentException: Could not resolve placeholder 'param.date' in value "#{new java.text.SimpleDateFormat('dd-MM-yyyy').parse('${param.date}')}"
How can I tell the compiler that the aforementioned field is from an argument? And, if the jar is made then, how do I inject the field when running the jar using command line arguments?
Thank you!
Solution
I cannot reproduce it. Try this:
java -jar [target].jar --param.date=01-11-2021
If tests failing, try @SpringBootTest(args = "--param.date=01-11-2021")
.
Refs:
By default,
SpringApplication
converts any command line option arguments (that is, arguments starting with--
, such as--server.port=9000
) to aproperty
and adds them to the SpringEnvironment
.
Answered By - DEWA Kazuyuki - 出羽和之
Answer Checked By - David Marino (JavaFixing Volunteer)