nested exception is org.springframework.core.convert.ConverterNotFoundException
【問題現象】
最近遇到一個很神奇的問題,報錯如下:
nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.util.HashMap<?, ?>] to type [java.lang.String]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
原因是個人在application.properties定義了一個變量 cpu.usage.upper.limit,沒有注意到配置文件中已經有一個變量,名為 cpu.usage
cpu.usage=70
cpu.usage.upper.limit=90
程序在引用變量 cpu.usage 的時候,就報了上述錯誤。
@Value("${cpu.usage:}") private String cpuUsage ;
【問題原因】
程序會認為 cpu.usage.upper.limit 是 cpu.usage 的子項,導致解析錯誤。
【解決方案】
將其中一個變量名修改一下,保證二者沒有包含關系即可。
比如:將變量 cpu.usage.upper.limit 改成 cpuUsage.upper.limit