2016-01-13 51 views
0

我想从我的application.yml配置文件注入一个映射列表到我的Spring Boot服务。这里是application.yml配置:从application.yml注入地图列表到服务

devoxx: 
    cfpApis: 
    - 
     url: http://cfp.devoxx.be/api/conferences 
     youtubeChannelId: UCCBVCTuk6uJrN3iFV_3vurg 
    - 
     url: http://cfp.devoxx.fr/api/conferences 
    - 
     url: http://cfp.devoxx.ma/api/conferences 
     youtubeChannelId: UC6vfGtsJr5RoBQBcHg24XQw 
    - 
     url: http://cfp.devoxx.co.uk/api/conferences 
    - 
     url: http://cfp.devoxx.pl/api/conferences 

这里是我在我的服务属性:

@Value("devoxx.cfpApis") 
List<Map<String,String>> cfpApis 

但是,必须有一些错误,因为当我尝试运行我的申请,我得到的以下例外:

java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map]: no matching editors or conversion strategy found 

任何想法我做错了什么?我试图将一个Grails 3项目迁移到一个vanilla Spring Boot项目中,这个配置在Grails 3中工作,但Grails有它自己的YAML处理器。

+1

到目前为止,我还没有使用'yaml'文件,但是您通常使用'@'''像'@Value(“$ {devoxx.cfpApis}”)''引用一个属性占位符。尽管如此,[spring documentation](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-loading- yaml)似乎表明使用'@ ConfigurationProperties'有一些不同的方法。也许[这个其他问题](http://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml)可以有一定的帮助 – Morfic

+0

这很好。谢谢。我会添加一个答案详述这个 – Sebastien

+0

干杯,很高兴我可以帮助:) – Morfic

回答

3

感谢@ Morfic的评论,这里是我最终如何解决这个问题。

我用@ConfigurationProperties(prefix="devoxx")注解标记了我的服务类。在我的服务中,我现在有一个名为cfpApis的属性,其中包含以下声明:

List<Map<String,String>> cfpApis 

而且这个工作很好。