简体   繁体   English

groovy MissingMethodException-通过ConfigSlurper进行解析

[英]groovy MissingMethodException - parsing through ConfigSlurper

I am trying to parse through some properties file using configslurper. 我正在尝试使用configslurper解析一些属性文件。

ENT.adminserver.nodenumber=1
ENT.managedserver.1.host=vserver04
ENT.managedserver.2.host=vserver05
ENT.managedserver.3.host=vserver08
ENT.managedserver.4.host=vserver07

Said properties file. 表示属性文件。 I am trying to read the host names from the properties. 我正在尝试从属性中读取主机名。

Properties properties = new Properties()
File propertiesFile = new File('DomainBuild.properties')
propertiesFile.withInputStream {properties.load(it)}
def config = new ConfigSlurper().parse(properties)

    def domainname="ENT" //will be passed through paremeters
    def domain = config.get(domainname)
    def managedServerFlow= {
      println domain.managedserver
      println domain.managedserver.keySet()
      domain.managedserver.each { 
        println it.getClass()
        println it.get("1") 
      }

      for (server in domain.managedserver) {
        println server.getClass()
        println server
      }
    }
}

the it.get("1") is causing the following error. it.get(“ 1”)导致以下错误。

No signature of method: java.util.LinkedHashMap$Entry.get() is applicable for argument types: (java.lang.String) values: [1]
Possible solutions: getAt(java.lang.String), grep(), grep(java.lang.Object), wait(), getKey(), any()

I looked through the java and groovy doc and spent few hours without resolution. 我浏览了Java和groovy文档,花了几个小时没有解决问题。 Please help. 请帮忙。

Instead of 代替

println it.get("1") 

Try 尝试

println it.'1'

Or 要么

println it.getAt("1") // as the exception shows you

Think about what types you are working with. 考虑一下您正在使用哪种类型。 config is a ConfigObject , which you can treat like a map. configConfigObject ,您可以将其视为地图。 Its sub-objects domain and domain.managedserver are also ConfigObjects. 它的子对象domaindomain.managedserver也是ConfigObjects。 When you call each on domain.managedserver and pass it a closure that takes no parameters, it gives you a set of Entries. 当您在domain.managedserver上调用each domain.managedserver并将其传递给不带任何参数的闭包时,它将为您提供一组条目。 Therefore you can't call it.get("1") because an Entry doesn't have a property called "1". 因此,您不能调用它it.get("1")因为Entry没有名为“ 1”的属性。 It has key and value . 它具有keyvalue So you can either println "$it.key: $it.value" or 因此,您可以println "$it.key: $it.value"

  domain.managedserver.each { key, value ->
    println value.getClass()
    println "$key: $value"
  }

or if you want to get the value for key "1" directly: 或者如果您想直接获取键“ 1”的值:

  println domain.managedserver.'1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM