Constantin Muraru opened SPR-16283 and commented
We are not able to use java.util.Duration as @Value
configuration in Spring Boot.
Sample project that exhibits this problem here: https://github.com/costimuraru/spring-boot-sample Spring Boot version: 1.5.9.RELEASE
@RestController
public class HelloController {
@Value("${mykey}")
Duration value;
@RequestMapping("/")
public String index() {
return "Greetings at " + value;
}
}
...
public class MyConfigPropertySource extends PropertySource {
MyConfigPropertySource(String name) {
super(name);
}
@Override
public Object getProperty(String path) {
return "mykey".equals(path) ? Duration.ofMinutes(1) : null;
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloController': Injection of autowired dependencies failed; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.time.Duration] to type [java.lang.String] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
This error is totally unexpected: No converter found capable of converting from type [java.time.Duration] to type [java.lang.String]
. We are not using String in neither the @Value
field nor in the return statement from the PropertySource. So why is Spring Boot trying to convert it to String? Is there any way around this to make it work?
The use case at hand is to create a PropertySource for typesafe (https://github.com/lightbend/config) which has support for Duration.
No further details from SPR-16283
Comment From: SharanyaMBhat
Is this issue still valid or can it be closed?
Comment From: gMan1990
Is this issue still valid or can it be closed?
@Configuration
public class MyJsonConfigParse extends DefaultJsonConfigParse implements WebMvcConfigurer {
@Override
public Map<String, Object> parse(String configText) {
return JSON.parseObject(configText);
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(JSONObject.class, String.class, JSONObject::toString);
registry.addConverter(JSONArray.class, String.class, JSONArray::toString);
}
}
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.alibaba.fastjson.JSONObject] to type [java.lang.String]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
at org.springframework.core.env.AbstractPropertyResolver.convertValueIfNecessary(AbstractPropertyResolver.java:265)
at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver.getProperty(ConfigurationPropertySourcesPropertyResolver.java:81)
at org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertyResolver.getProperty(ConfigurationPropertySourcesPropertyResolver.java:60)
at org.springframework.core.env.AbstractEnvironment.getProperty(AbstractEnvironment.java:594)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:153)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$1.getProperty(PropertySourcesPlaceholderConfigurer.java:149)
at org.springframework.core.env.PropertySourcesPropertyResolver.getProperty(PropertySourcesPropertyResolver.java:85)
at org.springframework.core.env.PropertySourcesPropertyResolver.getPropertyAsRawString(PropertySourcesPropertyResolver.java:74)
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:153)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
Comment From: snicoll
Sorry this got overlooked. Injecting a Duration
works. I am not sure about the bit in MyConfigPropertySource
. It should not return a duration like that.