I have been happily using @Value
to inject command line parameters into Spring Boot CommandLineRunner based programs. i.e.
java -jar myJar.jar --someParm=foo
... works just fine with classes containing:
@Autowired
public MyBean(@Value("someParm") String someParm) { ... }
However I'm now seeing this fail when the parm is not a String.
Here is my bean:
@Component
class MyBean {
private final LocalDate date;
@Autowired
public MyBean (@Value("date") @DateTimeFormat(iso=ISO.DATE) LocalDate date) {
this.date = date;
}
public void hello() {
System.out.println("Hello on " + date);
}
}
... and my application class:
@SpringBootApplication
public class MyApp implements CommandLineRunner {
@Autowired
private MyBean myBean;
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
@Override
public void run(String... args) throws IOException {
myBean.hello();
}
}
When I run it as java -jar MyApp.java --date=2016-12-10
, I get a stack trace ending:
java.lang.IllegalStateException: Cannot convert value of type
[java.lang.String] to required type [java.time.LocalDate]:
no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:302)
Although the docs state that there is a standard converter for String->Date, I have experimented with registering my own, and hit the same NullPointerException as this post: How to register custom converters in spring boot?
What can I do to make this work?
Java 8, Spring Boot 1.3.5-RELEASE