【问题标题】:Inject a list of maps from application.yml into service将 application.yml 中的地图列表注入服务
【发布时间】:2016-04-18 17:21:54
【问题描述】:

我正在尝试将我的 application.yml 配置文件中的映射列表注入到我的 Spring Boot 服务中。这是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 项目迁移到普通 Spring Boot 项目中,并且此配置在 Grails 3 中有效,但 Grails 有自己的 YAML 处理器。

【问题讨论】:

  • 到目前为止我还没有使用过yaml 文件,但您通常使用$ 来引用属性占位符,例如@Value("${devoxx.cfpApis}")。尽管如此,spring documentation 似乎暗示了一种使用@ConfigurationProperties 的不同方法。也许this other question 可以提供一些帮助
  • 这很好用。谢谢。我将添加一个详细说明此问题的答案
  • 干杯,很高兴我能帮上忙 :)

标签: groovy spring-boot yaml


【解决方案1】:

感谢@Morfic 的评论,这就是我最终解决该问题的方法。

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

List<Map<String,String>> cfpApis

这很好用。

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 2014-09-15
    • 2020-03-01
    • 2019-05-09
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2019-06-21
    • 2016-05-09
    相关资源
    最近更新 更多