Grails YAML 映射列表
Grails YAML list of maps
在我的 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
但是当我尝试使用以下代码在我的服务中加载此配置时,apiConfig 为空:
def apiConfig = grailsApplication.config.getProperty("tvoxx.cfpApis")
当应用程序启动并且我的 YAML 代码在 http://yaml-online-parser.appspot.com/ 上正确解析时,我没有收到任何错误,所以我不知道出了什么问题。
只是为了确认我们在 Slack 上讨论的内容。
使用 grailsApplication.config.getProperty("tvoxx.cfpApis")
,Grails 将尝试查找类型 String
的值,因为您的值是 Map null 将被 returned.
你必须明确地告诉你期望的类型,使用:
grailsApplication.config.getProperty("tvoxx.cfpApis", Map)
其他方法是使用 getAt()
方法,其中对象是 returned,所以你可以使用
grailsApplication.config.tvoxx.cfpApis
获取值。
第一个可能更适合 .java
和 @CompileStatic
但对于标准 .groovy
class 后者语法更简单。请注意不存在的键,因为它将 return 为空 ConfigObject
而不是 null
,例如 ?.toString()
方法将导致 'ConfigObject@123123
而不是共 null
在我的 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
但是当我尝试使用以下代码在我的服务中加载此配置时,apiConfig 为空:
def apiConfig = grailsApplication.config.getProperty("tvoxx.cfpApis")
当应用程序启动并且我的 YAML 代码在 http://yaml-online-parser.appspot.com/ 上正确解析时,我没有收到任何错误,所以我不知道出了什么问题。
只是为了确认我们在 Slack 上讨论的内容。
使用 grailsApplication.config.getProperty("tvoxx.cfpApis")
,Grails 将尝试查找类型 String
的值,因为您的值是 Map null 将被 returned.
你必须明确地告诉你期望的类型,使用:
grailsApplication.config.getProperty("tvoxx.cfpApis", Map)
其他方法是使用 getAt()
方法,其中对象是 returned,所以你可以使用
grailsApplication.config.tvoxx.cfpApis
获取值。
第一个可能更适合 .java
和 @CompileStatic
但对于标准 .groovy
class 后者语法更简单。请注意不存在的键,因为它将 return 为空 ConfigObject
而不是 null
,例如 ?.toString()
方法将导致 'ConfigObject@123123
而不是共 null