Grails YAML 映射列表

Seb*_*ien 3 configuration grails yaml

在我的 Grails 3 application.yml 中,我定义了一个映射列表,如下所示:

tvoxx:
    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
Run Code Online (Sandbox Code Playgroud)

但是当我尝试使用以下代码在我的服务中加载此配置时,apiConfig 为空:

def apiConfig = grailsApplication.config.getProperty("tvoxx.cfpApis")
Run Code Online (Sandbox Code Playgroud)

当应用程序启动并且我的 YAML 代码在http://yaml-online-parser.appspot.com/ 上正确解析时,我没有收到任何错误,所以我不知道出了什么问题。

dro*_*ggo 5

只是为了确认我们在 Slack 上讨论的内容。

使用grailsApplication.config.getProperty("tvoxx.cfpApis"),Grails 将尝试查找类型的值,String并且因为您的值是 Map null 将被返回。

你必须明确地告诉你期望什么类型,使用: grailsApplication.config.getProperty("tvoxx.cfpApis", Map)

另一种方法是使用getAt()方法,其中返回对象,因此您可以使用 grailsApplication.config.tvoxx.cfpApis获取值。

第一个可能更好.java@CompileStatic但对于标准.groovy类,后者的语法更简单。请注意不存在的键,因为它将返回空ConfigObject而不是null,例如?.toString()方法将导致'ConfigObject@123123而不是null