在我的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为null:
def apiConfig = grailsApplication.config.getProperty("tvoxx.cfpApis")
当应用程序启动并且我的YAML代码在http://yaml-online-parser.appspot.com/所以我不知道怎么了。
只是为了确认我们在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